堆和栈深入研究(看看国外是怎么解释的,包含c java 及系统三方面来看)

上午看某文章时候涉及缓冲区溢出的问题,谈到C的栈和堆,有所不懂于是baidu了一下发现论坛上的解释都较为凌乱,google一下后发现国外大学的Lecture Notes 中有不少的说明,下面简单的摘录三段,一是c中的,二是对于java的,三是从os角度看的。Stack vs Heap AllocationHow the memory of the computer is organized for a running program? When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system. The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack. This can be seen in Figure 14.13. When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(), as seen in Figure 14.13(a). If main() calls a function, func1(), additional storage is allocated for the variables in func1() at the top of the stack as shown in Figure 14.13(b). Notice that the parameters passed by main() to func1() are also stored on the stack. If func1() were to call any additional functions, storage would be allocated at the new Top of stack as seen in the figure. When func1() returns, storage for its local variables is deallocated, and the Top of the stack returns to to position shown in Figure 14.13(c). If main() were to call another function, storage would be allocated for that function at the Top shown in the figure. As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage. The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage. 小结:Stack: automatic variables within functionsHeap: global variables (storage class external), and static variables ============================In java 情况如下(1)The stack is the program memory area, so all your primitive type variables and the memory adress of your objects are written on the stack. It is a fast access valuable memory area.The heap is where the VM keeps the objects, and it is a huge amount of memory. When you create an object, the VM puts the object in the HEAP and puts the adress of the object created on the STACK.(2)There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory. An object reference on the stack is only an address that refers to the place in heap memory where that object is kept. It is useful to know that these two different kinds of memory exist in Java. Stack memory is the program's memory, and heap memory resides outside of the program.这好像有点跟C的不同(相反)。引入一点垃圾回收机制的知识When you need a new object, Java allocates the required memory. When you are done with an object, the memory is reclaimed for you automatically via Java's garbage collection facility.Garbage collection runs as a thread in the background, looking for objects that no longer have a usable reference. When it finds them, it destroys them and reclaims the memory.The implementation of garbage collection varies between Java Virtual Machines. They generally follow the same process, however. First, the garbage collector gets a snapshot of all running threads and all loaded classes. Then, all objects that are referred to by this thread set are marked as current. The process stops when all objects that it is possible to reach have been marked and the rest have been discarded.In order to help the Virtual Machine, it is a good idea to remove your references to unneeded objects. This is often done by simply setting your reference to null:Test t = new Test();t.someAction();// all donet = null;小结:Stack: Primitive data types(primitive types), the addresses of objects(=references).Heap: objects. ===============================================从系统的角度看 stack(栈)和heap(堆)Dynamic Data Structures: The HeapA typical personal computer or workstation today has somewhere between 16 and 64 megabytes of RAM installed. Using a technique called virtual memory, the system can swap pieces of memory on and off the machine's hard disk to create an illusion for the CPU that it has much more memory, for example 200 to 500 megabytes. While this illusion is complete as far as the CPU is concerned, it can sometimes slow things down tremendously from the user's perspective. Despite this drawback, virtual memory is an extremely useful technique for "increasing" the amount of RAM in a machine in an inexpensive way. Let's assume for the sake of this discussion that a typical computer has a total memory space of, for example, 50 megabytes (regardless of whether that memory is implemented in real RAM or in virtual memory). The operating system on the machine is in charge of the 50-megabyte memory space. The operating system uses the space in several different ways, as shown here: The operating system and several applications, along with their global variables and stack spaces, all consume portions of memory. When a program completes execution, it releases its memory for reuse by other programs. Note that part of the memory space remains unused at any given time. This is, of course, an idealization, but the basic principles are correct. As you can see, memory holds the executable code for the different applications currently running on the machine, along with the executable code for the operating system itself. Each application has certain global variables associated with it. These variables also consume memory. Finally, each application uses an area of memory called the stack, which holds all local variables and parameters used by any function. The stack also remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped." Because of this, the size of a program's stack fluctuates constantly as the program is running, but it has some maximum size. As a program finishes execution, the operating system unloads it, its globals and its stack space from memory. A new program can make use of that space at a later time. In this way, the memory in a computer system is constantly "recycled" and reused by programs as they execute and complete. In general, perhaps 50 percent of the computer's total memory space might be unused at any given moment. The operating system owns and manages the unused memory, and it is collectively known as the heap. The heap is extremely important because it is available for use by applications during execution using the C functions malloc (memory allocate) and free. The heap allows programs to allocate memory exactly when they need it during the execution of a program, rather than pre-allocating it with a specifically-sized array declaration.

-----------------------------------==================残忍的分割线===================-----------------------

大译如下:

栈和堆allocationhow计算机的存储器被组织为一个正在运行的程序?当一个程序被加载到内存中,它分为记忆,三区称为段:代码段,堆栈段,和堆段。文本段(有时也被称为代码段)是已编译的程序代码本身停留。这是程序的步骤进行,机器语言表示的所有功能,包括制定计划,用户定义和系统。剩下的两个领域的系统内存存储可以通过为数据存储编译器分配。栈内存分配函数中的自动变量。栈是先出最后的(LIFO)存储设备的新的存储分配和释放在只有一个` `端”,称为堆栈的顶部。这在图14.13中可以看到。当一个程序开始执行的功能main(),空间被分配在栈中声明的所有变量main(),如图14.13(a)。如果main()调用一个函数,func1(),额外的存储空间分配给变量func1()位于堆栈顶部的如图14.13(b)。注意,通过对func1() main()参数也存储在堆栈。如果func1()被调用任何额外的功能,存储将分配在堆栈的最新的图中可以看出。func1()返回时,它的局部变量的存储和释放,栈顶的位置返回到图14.13(c)。如果main()被调用另一个函数,存储分配的功能在图中所示的顶部。可以看到,内存分配在栈区是在程序执行过程中使用和重用。它应该是明确的,在这一地区分配存储器将包含从以前的使用剩下的垃圾值。堆段提供的数据存储的程序更稳定;分配在堆内存中依然存在的一个程序的时间。因此,全局变量(存储类外部),变量和静态变量在堆中分配。在堆中分配的内存,如果初始化为零的程序开始时是零,直到程序使用它。因此,不需要包含垃圾堆。小结:栈:自动变量在functionsheap:全局变量(存储类外部),变量和静态变量在Java ============================情况如下(1)堆栈的程序存储区,所以你所有的原始类型的变量和对象的地址写在堆栈存储器。这是一个快速的获取有价值的存储区。堆在VM保持对象,它是一个巨大的内存量。当你创建一个对象,VM将堆中的对象将在堆栈中创建的对象的地址。(2)有两种用Java内存。这些被称为堆栈和堆内存。堆栈存储器存储原始数据类型和对象的地址。对象的值存储在堆内存。堆栈上的一个对象的引用是唯一的一个地址,是指发生在堆内存在哪里保存对象。它是要知道在Java存在这两种不同的记忆有用。栈内存程序的内存,和堆内存驻留程序之外。这好像有点跟C的不同(相反引入一点垃圾回收机制的知识)。当你需要一个新的对象,Java分配需要的内存。当你完成了一个对象,内存回收你的垃圾收集设施,通过Java自动。垃圾收集运行在后台线程,寻找那不再有一个可用的参考对象。当它发现他们,破坏他们回收内存。垃圾收集的Java虚拟机之间实现变化。他们一般都遵循相同的过程,但是。第一,垃圾收集器获取快照的所有正在运行的线程和所有已加载的类。然后,被该线程组的所有对象被标记为电流。这个过程停止时,所有的对象,它是有可能达到的已标记和其余的被丢弃。为了帮助虚拟机,这是一个好主意,把你不需要的对象的引用。这通常是通过简单地设置为你做参考:试验T =新test();T someaction();/ / DONet = null;小结:栈:原始数据类型(原始类型),对象的地址(=参考堆:对象)。===============================================从系统的角度看栈(栈)和堆(堆)动态数据结构的heapa:典型的个人计算机或工作站今天有大约16和64 MB的RAM之间安装。使用的技术称为虚拟内存,系统可以互换和关闭机器的硬盘的CPU,它有更多的内存创建一个幻觉记忆的碎片,例如200到500 MB。而这个错觉


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值