分享

java堆与非堆的一些研究

 hh3755 2012-06-26
java有堆内存,可以通过-xms -xmx来进行控制,当然使用该参数控制的也只是堆内存.
1. 为什么启动tomcat后我设置的最大xmx为64,但实际可能占用128
原因:内存说明见下:

Heap Memory Pool 
A. Eden Space(heap):大多数物件初始化时从Eden Space池分配记忆体,即是存在于此池中 
B. Survivor Space(heap):此池包含的物件是那些原先在eden space中,但是已经经历过垃圾回收而仍然存在的物件。 
C. Tenured Generation(heap):在surviver space中已经存在了一段时间之后的物件会移动到这个池中。

Non-Heap Memory Pool 
D. Code Cache (non-heap):储存编译的程式码和local variables。 
E. Permanent Generation(non-heap):包含虚拟机器自身的所有反射资料。 比如classmothod物件。 对于使用class data sharing的JVM,分为唯读(shared-ro)读写(shared-rw)两个区域。

由上可知,jvm除占用堆内存外还占用了非堆内存,该非堆内存可能占用的和堆内存一样大.
2.非堆内存会垃圾回收吗?
可以:

Can the permanent generation be garbage collected?

Yes it can. There is often the misconception that string constants cannot be garbage collected. However the only requirement that the JVM specification stipulates is that identity comparison works for constant string values. There is no requirement that the constant be the same object throughout the lifetime of the JVM. Hence if there are no references left to a string constant, there is no reason why it cannot be garbage collected.

To prove this take the following code:

 
public class PermGenDemo {
  public static void main(String[] args) {
    int i = 0;
    while (true) {
      ("string-" + ++i).intern();
    }
  }
}

If the above code is run using the following command:

 
$ java -verbose:gc -XX:PermSize=8m -XX:MaxPermSize=64m PermGenDemo

The following output will be seen (note output cut for brevity):

 
[Full GC [PSYoungGen: 32K->0K(28928K)] . . . [PSPermGen: 65535K->6461K(65536K)]]
[Full GC [PSYoungGen: 32K->0K(31168K)] . . . [PSPermGen: 65535K->6461K(65536K)]]
The above conclusively shows that the permanent generation is collected and when it requires collecting, triggers a full collection.
 
另有一张清晰的图可以说明:
3.非堆内存里面还有什么?
非堆内存里面还有code cache,正在研究如何控制他们.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多