JVM实际分配的内存以及它们之间有何不同? [英] Actual memory allocation by JVM and how do they differ?

查看:93
本文介绍了JVM实际分配的内存以及它们之间有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来有很多问题,但是它们都是相互关联的.我有点困惑,例如分配堆空间在哪里以及堆栈内存在哪里?
如果两者都存在于主存储器中,那么为什么会说堆栈存储器更易于访问,为什么我们不能在堆栈存储器中分配对象呢?
因为类存储在PermGen中,所以该空间在哪里分配?它与堆空间有何区别?常量字符串在哪里存储?

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 ?

推荐答案

  1. 堆和堆栈分配在哪里?" 对此问题的可接受答案涵盖了这一点.每个线程都会它自己的堆栈,它们都共享一个堆.操作系统控制堆栈和堆项目的确切内存位置,并且有所不同.
  2. 为什么堆栈内存更易于访问"每个线程都有自己的堆栈,因此并发问题更少.堆栈和堆都可以在内存层次结构的 L1,L2和L3部分进行缓存 a>,所以我不同意Daniel在这里的回答.真的,我不会说一种内存比另一种内存特别容易访问.
  3. 为什么我们不能在堆栈存储器中分配对象?"这是JVM做出的设计决定.在其他语言(如C/C ++)中,您可以在堆栈上分配对象.从分配了该堆栈框架的函数返回后,此类对象将丢失. C/C ++程序中常见的错误源是共享指向此类堆栈分配对象的指针.我敢打赌,这就是为什么JVM设计人员做出此选择的原因,尽管我不确定.
  4. PermGen是堆的另一部分.在JVM的生命周期中,常量字符串存储在此处.它像堆其余部分一样收集垃圾.
  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. 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. In other languages like C/C++ you can allocate objects on the stack. 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. I bet that's why the JVM designers made this choice, though I am not sure.
  4. The PermGen is another piece of the heap. Constant strings are stored here for the lifetime of the JVM. It is garbage collected just like the rest of the heap.

这篇关于JVM实际分配的内存以及它们之间有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆