请问heap和stack有什么区别?

Patrick_DK 2001-12-17 02:11:09
...全文
3239 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Patrick_DK 2001-12-18
  • 打赏
  • 举报
回复
pushpushpush
magus_yang 2001-12-18
  • 打赏
  • 举报
回复
英文不难,意思也通俗
magus_yang 2001-12-18
  • 打赏
  • 举报
回复
Stack. This lives in the general RAM (random-access memory) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to registers. The Java compiler must know, while it is creating the program, the exact size and lifetime of all the data that is stored on the stack, because it must generate the code to move the stack pointer up and down. This constraint places limits on the flexibility of your programs, so while some Java storage exists on the stack ?in particular, object handles ?Java objects are not placed on the stack.

Heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The nice thing about the heap is that, unlike the stack, the compiler doesn't need to know how much storage it needs to allocate from the heap or how long that storage must stay on the heap. Thus, there's a great deal of flexibility in using storage on the heap. Whenever you need to create an object, you simply write the code to create it using new and the storage is allocated on the heap when that code is executed. And of course there's a price you pay for this flexibility: it takes more time to allocate heap storage.
Patrick_DK 2001-12-18
  • 打赏
  • 举报
回复
楼上的,这个我know,我不太清楚的是这两个的运行机制,以及有什么不同啊?
magus_yang 2001-12-18
  • 打赏
  • 举报
回复
在C/C++中,数组都是放在stack中的,而动态分配的(如:malloc)是在heap中。
在java中,所有用new创建的对象都是放在heap中的,数组也是对象。
Patrick_DK 2001-12-17
  • 打赏
  • 举报
回复
能讲详细点吗?
magus_yang 2001-12-17
  • 打赏
  • 举报
回复
前者动态,后者静态

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧