简体   繁体   English

JVM的实际内存分配以及它们有何不同?

[英]Actual memory allocation by JVM and how do they differ?

This might seem a lot of questions but they are all interrelated.I'm little confused as in where is the heap space allocated and where is the stack memory located ? 这可能看起来很多问题,但它们都是相互关联的。我很困惑,因为分配的堆空间在哪里以及堆栈内存位于何处?
If both are present in main memory then why it is said that stack memory is easier to access and why can't we allocate objects in stack memory ? 如果两者都存在于主存储器中那么为什么说堆栈存储器更容易访问,为什么我们不能在堆栈存储器中分配对象?
Since classes are stored in PermGen where is this space allocated and how does it differ from heap space and where are constant strings stored ? 由于类存储在PermGen中,这是否分配了这个空间,它与堆空间有什么不同?存储常量字符串的位置是什么?

  1. "Where are the heap and stack allocated?" “堆和堆栈在哪里分配?” The accepted answer to this question covers this. 这个问题公认答案涵盖了这一点。 Each thread gets its own stack and they all share one heap. 每个线程都有自己的堆栈,它们共享一个堆。 The operating system controls the exact memory locations of the stacks and heap items and it varies. 操作系统控制堆栈和堆项的确切内存位置,并且它会有所不同。
  2. "Why is stack memory easier to access" Each thread has its own stack, so there are fewer concurrency issues. “为什么堆栈内存更容易访问”每个线程都有自己的堆栈,因此并发问题较少。 The stack and heap are both eligible for caching in the L1, L2, and L3 portions of the memory hierarchy , so I disagree with Daniel's answer here. 堆栈和堆都有资格在内存层次结构L1,L2和L3部分进行缓存,所以我不同意Daniel的回答。 Really I would not say that one kind of memory is particularly easier to access than the other. 真的,我不会说一种内存比另一种更容易访问。
  3. "Why can't we allocated objects in stack memory?" “为什么我们不能在堆栈内存中分配对象?” This is a design decision taken by the JVM. 这是JVM采取的设计决策。 In other languages like C/C++ you can allocate objects on the stack. 在其他语言(如C / C ++)中,您可以在堆栈上分配对象。 Once you return from the function that allocated that stack frame such objects are lost. 从分配该堆栈帧的函数返回后,此类对象将丢失。 A common source of errors in C/C++ programs is sharing a pointer to such a stack allocated object. C / C ++程序中常见的错误来源是共享指向这种堆栈分配对象的指针。 I bet that's why the JVM designers made this choice, though I am not sure. 我敢打赌,这就是JVM设计师做出这个选择的原因,尽管我不确定。
  4. The PermGen is another piece of the heap. PermGen是堆的另一部分。 Constant strings are stored here for the lifetime of the JVM. 在JVM的生命周期中,常量字符串存储在此处。 It is garbage collected just like the rest of the heap. 就像堆的其余部分一样被垃圾收集。

If both are present in main memory then why it is said that stack memory is easier to access 如果两者都存在于主存储器中,那么为什么说堆栈存储器更容易访问

There's speed of access and speed of allocation. 有访问速度和分配速度。 Stack allocation (as in alloca ) is fast because there's no need to search for an unused block of memory. 堆栈分配(如在alloca )很快,因为不需要搜索未使用的内存块。 But Java doesn't allow stack allocation, unless you count the allocation of new stack frames. 但Java不允许堆栈分配,除非您计算新堆栈帧的分配。

Accessing stack memory is fast because it tends to be cached. 访问堆栈内存很快,因为它往往是缓存的。 Not only are locals near one another, they are also stored very compactly. 当地人不仅彼此靠近,而且也非常紧凑地存放。

and why can't we allocate objects in stack memory ? 为什么我们不能在堆栈内存中分配对象?

This would be useful, but dangerous. 这将是有用的,但很危险。 A user could allocate an object on the stack, create references to it from permanent objects, and then try to access the object after the associated stack frame is gone. 用户可以在堆栈上分配对象,从永久对象创建对它的引用,然后在关联的堆栈帧消失后尝试访问该对象。

It's safe to store primitives on the stack because we can't create references to them from elsewhere. 基元存储在堆栈上是安全的,因为我们无法从其他地方创建对它们的引用。

Since classes are stored in PermGen where is this space allocated and how does it differ from heap space and where are constant strings stored ? 由于类存储在PermGen中,这是否分配了这个空间,它与堆空间有什么不同?存储常量字符串的位置是什么?

PermGen is just another heap space. PermGen只是另一个堆空间。 String literals are stored in the literal pool, which is just a table in memory which is allocated when a class is loaded. 字符串文字存储在文字池中,文字池只是内存中的一个表,在加载类时分配。

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

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