岳翎现在的样子:heap和stack有什么区别

来源:百度文库 编辑:中科新闻网 时间:2024/05/10 07:24:34
heap和stack有什么区别

栈是一种线形集合,其添加和删除元素的操作应在同一段完成。栈按照后进先出的方式进行处理。
堆是栈的一个组成元素

1.heap是堆,stack是栈。2.stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的,heap常用new关键字来分配。3.stack空间有限,heap的空间是很大的自由区。在Java中,若只是声明一个对象,则先在栈内存中为其分配地址空间,若再new一下,实例化它,则在堆内存中为其分配地址。4.举例:数据类型 变量名;这样定义的东西在栈区。如:Object a =null; 只在栈内存中分配空间new 数据类型();或者malloc(长度); 这样定义的东西就在... [详细]

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.
堆栈。这生活在一般的RAM(随机存取存储器)区域,但具有通过其堆栈指针从处理器的直接支持。堆栈指针向下移动到创建新的内存和释放内存。这是一个非常快速和有效的方式来分配存储,仅次于寄存器。 Java编译器必须知道,而创建的程序,所有的数据存储在栈上的确切大小和生命周期,因为它必须生成代码,以便上下移动堆栈指针。这个约束限制你的程序的灵活性,因此,虽然存在一些Java存储在堆栈上,特别是对象的句柄?Java对象未放置在堆栈中。

堆。这是一个通用的内存池(在RAM区)的所有Java对象的生活。这种做法的好处是,不同的堆栈,编译器并不需要知道它需要从堆中,要不了多久,存储系统必须保持在堆上分配多少存储空间的堆。因此,在使用存储在堆中有一个很大的灵活性。当你需要创建一个对象,你只需要编写代码来创建它使用新的和存储在堆上分配时执行该代码。当然,有一个你付出的代价提供这种灵活性:它需要更多的时间来分配堆存储。

对象的名柄存放在堆栈上,频繁的新增和删除(比如方面里的局部变量)
对象存放在堆上,相对于对象的名柄,存活周期长得多,有些对象会在程序结束时才从堆上释放