heap stack区别简介

转载 2012年03月28日 00:05:27
You can imagine the stack as being like a stack of plates. As soon as a function calls some other function things get added to the stack (from the bottom, upwards) starting with the return address (i.e. where the program flow should go back to, once the called function has finished).

Stack memory
Let's suppose there was nothing on the stack to begin with. One of the functions (either the calling function or the called function) would start a stack by placing a return address - and some extra 'plates' might be added, representing any arguments that get passed to the called function (the called function's parameters).

Program execution then gets passed to the called function which probably adds some more plates in the form of 'local variables'. Every 'plate' has some sort of information written on it that means something special to te program. By the time the called function reaches its return statement it has a big stack of plates. Just before it returns, it removes the plates (one by one) from the top downward so that it can read the return address, written on that very bottom plate. The program then jumps back to that original return address.

The most important thing about the stack is that you don't need to think much about it. Your program manages it. Also, stack management is very efficient because the plates don't literally need to be removed (like they would if they were real plates). In reality, the program contains a "stack pointer" that simply gets updated each time a particular plate needs to be written to or read from. The size of the stack is usually quite small because it's mostly used for local variables (variables that go out of scope pretty soon after they're finished with). The size of the stack is set at compile time and is small enough to be available on any computer, no matter how little RAM it's got.

Heap memory
The heap is a different thing altogether. Different PC's have different amounts of memory so it would be silly to have to allocate all the memory at compile time. If a particular computer has a lot of RAM, why not make use of it? Therefore the heap consists of memory that can be allocated at run time. The amount of heap memory is different for different machines and a well written program will take account of this (allocating heap memory sparingly - and not assuming that everyone will have massive amounts of it). Heap memory is most often used for 'large' things (e.g. the contents of a file) - or for things that need to 'stay around' in memory so that different functions can share the data (in other words, for NON-local variables). It's also used very frequently to allocate memory for things whose size is not known until run time. For example, if you had to open a file and read in the data, you don't know how much data exists until run time. In contrast, the size of objects allocated on the stack, needs to be known at compile time.

Whereas your program takes care of stack memory (allocating it & cleaning it up) it's the programmer's job to deal with heap memory. The size of objects allocated on the heap is often not known to the compiler - therefore the programmer must allocate and release the memory specifically.


  • 2015年12月18日 11:57
  • 104KB
  • 下载




heap和stack的区别 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。Java自动管理栈和堆,程序员不能直接地设置栈或堆。 每个应用程序运行时,都有属于自己的一段内存...
  • p_3er
  • p_3er
  • 2017年11月14日 11:56
  • 76


1.Java中对象都是分配在heap(堆)中。从heap中分配内存所消耗的时间远远大于从stack产生存储空间所需的时间。   (1)每个应用程序运行时,都有属于自己的一段内存空间,用于存放临时变...
  • suwu150
  • suwu150
  • 2016年11月01日 00:32
  • 153


java中堆(heap)和堆栈(stack)有什么区别  stack 和 heap 都是内存的一部分 stack 空间小,速度比较快, 用来放对象的引用 heap 大,一般所有创建的对象都放在这...


在Java里面Heap和Stack分别存储数据的不同。 Heap(堆) Stack(栈) JVM中的功能 内存数据区 ...


1.Java中对象都是分配在heap(堆)中。从heap中分配内存所消耗的时间远远大于从stack产生存储空间所需的时间。   (1)每个应用程序运行时,都有属于自己的一段内存空间,用于存放临时变...


一、预备知识―程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的...


一、预备知识—程序的内存分配     一个由C/C++编译的程序占用的内存分为以下几个部分     1、栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等。其 ...


1.Java中对象都是分配在heap(堆)中。从heap中分配内存所消耗的时间远远大于从stack产生存储空间所需的时间。  (1)每个应用程序运行时,都有属于自己的一段内存空间,用于存放临时变量、参...
您举报文章:heap stack区别简介