[英]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中,这是否分配了这个空间,它与堆空间有什么不同?存储常量字符串的位置是什么?
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.