简体   繁体   English

当我在程序中创建堆栈时,为什么它存储在堆中而不是堆栈中?

[英]When i create a stack in the program, why it is storing in heap not stack?

If I create a stack in a java method using Deque<Integer> stack = new LinkedList<Integer>(); 如果我使用Deque<Integer> stack = new LinkedList<Integer>();在java方法中创建堆栈,则Deque<Integer> stack = new LinkedList<Integer>(); Why does this stack variable store in the heap but not in the stack? 为什么此堆栈变量存储在堆中而不存储在堆栈中?

Java Heap Space Java堆空间

Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Java运行时使用Java堆空间为Objects和JRE类分配内存。 Whenever we create any object, it's always created in the Heap space. 每当我们创建任何对象时,它总是在堆空间中创建。

Garbage Collection runs on the heap memory to free the memory used by objects that doesn't have any reference. 垃圾回收在堆内存上运行以释放没有任何引用的对象使用的内存。 Any object created in the heap space has global access and can be referenced from anywhere of the application. 在堆空间中创建的任何对象都具有全局访问权限,并且可以从应用程序的任何位置进行引用。


Java Stack Memory Java堆栈内存

Java Stack memory is used for execution of a thread. Java Stack内存用于执行线程。 They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. 它们包含短期的方法特定值,以及从该方法引用的对堆中其他对象的引用。

Stack memory is always referenced in LIFO (Last-In-First-Out) order. 堆栈存储器始终按LIFO(后进先出)顺序引用。 Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. 每当调用方法时,都会在堆栈存储器中创建一个新块,以容纳该方法的本地原始值并引用该方法中的其他对象。

As soon as method ends, the block becomes unused and become available for next method. 方法结束后,该块将立即变为未使用状态,并可用于下一个方法。 Stack memory size is very less compared to Heap memory. 与堆内存相比,堆栈内存的大小要小得多。


So when you use Deque stack = new LinkedList<>() , Deque stack is stack according to data model, but according to JVM this is just another object , like new Object() , new ArraList<>() etc. And these objects are stored in the heap . 因此,当您使用Deque stack = new LinkedList<>()Deque stack是根据数据模型进行堆栈的,但是根据JVM,这只是另一个对象 ,如new Object()new ArraList<>()等。这些对象被存储在堆中

See details in Java Heap Space vs Stack – Memory Allocation in Java Java堆空间与堆栈中查看详细信息– Java中的内存分配

Some languages like C allow you to choose whether to allocate memory on the stack or the heap. 诸如C类的某些语言允许您选择是在堆栈还是在堆上分配内存。 There are a number of attractive reasons why one might want to use the stack. 有许多吸引人的原因,为什么您想使用堆栈。 Memory allocated on the stack is automatically free d when the stack frame is "popped" (the function returns). 当“弹出”堆栈帧(函数返回)时,将自动free在堆栈上分配的内存。 You also get better performance because the stack is a contiguous chunk of memory, whereas memory can be allocated anywhere on the heap. 您还可以获得更好的性能,因为堆栈是连续的内存块,而内存可以分配到堆上的任何位置。 It's easier for the compiler to optimize. 编译器更容易优化。

However, like many aspects of C , here there be dragons. 但是,就像C许多方面一样,这里还有龙。 There is no possibility of thread safety or sharing data. 线程安全或共享数据是不可能的。 Whatever you allocate on the stack necessarily can't survive beyond the life of the function call. 无论您在堆栈上分配的内容是什么,都不一定会超出函数调用的寿命。 Objects on the stack are freed when the function returns, so if you need to return an object from a function it must be allocated on the heap. 函数返回时,将释放堆栈上的对象,因此,如果需要从函数返回对象,则必须在堆上分配该对象。

Stack allocation can be dangerous and requires a low-level understanding of your program's memory usage. 堆栈分配可能很危险,并且需要对程序的内存使用情况有较低的了解。 These go against the design principles of Java, namely that you should be able to let the computer handle memory management for you. 这些违反了Java的设计原则,即您应该能够让计算机为您处理内存管理。 Therefore the language designers just decided to not let you do it. 因此,语言设计师只是决定不让您这样做。 As Oleg already shared, in Java all objects are allocated on the heap. 由于Oleg已经共享,因此在Java中,所有对象都分配在堆上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM