关于堆(Heap)栈(Stack)

03-03-21 navyzhu

请问各位大侠,Heap和Stack有何区别,在Java中是如何实现的,且性能上有何差异?

谢谢!

manbaum
2003-03-25 09:34

不知道 java 对 heap 及 stack 如何定义。在我对 C 的理解中,堆和栈是这样定义的。

堆和栈公用同一块 mem 区域,但堆指针初始时 mem 的首(低)地址,而栈指向 mem 的尾(高)地址。

如果从堆申请了一块内存,则堆指针向下(高处)移动;而从栈得到了内存,则栈指针向上(低处)移动。

只要 heap 指针小于 stack 指针,则说明内存尚未分配完,还有可用内存,否则就内存溢出错误。

原理如此,我想,具体的实现会有不同的方法,以便进行内存分配的优化。

Jevang
2003-03-26 14:48

This is really a broad topic to be covered in a short note. even in java. But I guess normally it's matter to developer only when you need decide memory setting and cope with GC.

All java objects live in heap, so be prudent when setting the -Xmx and -Xms for heapsize; GC will work with the objects in the heap.

Stack is mainly used by local variables and threads, it rarely a matter to me during system turning.

Just my 2 cents.

-Wanchun