JVM - Heap Heap Detailed

Heap (Heap)

There is only one instance of a JVM memory heap, the memory heap size can be adjusted.

After the class loader reads the class files need to classes, methods, often variables into the heap memory, all references to types of stored information is really to facilitate performs, heap memory is divided into three parts:

  • Yong Generation Space young generation: Young / New
  • Tenure Generation Space pension Area: Old / Tenure
  • Permanent Permanent Space Area: Perm

Java7 before:
Here Insert Picture Description

Java8:
After JDK1.8 permanent replacement for the metaspace on behalf of:

Here Insert Picture Description

Freshman class district is born, growing, dying area, where a class is generated, application, and finally collected the garbage collector, the end of life. Freshmen area is divided into two parts: Eden District (Eden Space) and survivors zone (Survivor space), all classes are again out of the Eden area is new.

District and two survived: 0 District (Survivoa 0 Space) and Zone 1 (Survivor 1 space). When Eden is running out of space, the program and the need to create an object, JVM's garbage collector will be garbage collection (Minor GC) Eden District, the Eden area of ​​the object is no longer referenced by other objects to be destroyed . Eden region then move the object to the remaining surviving 0 region. If the area is also full of survivors 0, then the garbage collection area, then moved to Zone 1. If Zone 1 is also full, then move to the pension area.

If the pension area is also full, so this time will result in Major GC (Full GC), for memory clean up the pension area. If the pension area after the implementation of the FUll GC found that the object still can not be saved, it will have abnormal OOM "OutOfMemoryError."

If java.lang.OutOfMemoryError appears: Java heap space anomaly, indicating that the Java Virtual Machine heap memory is not enough, there are two reasons:

  • Java Virtual Machine heap memory settings is not enough, you can adjust the parameters -Xms, -Xmx.
  • The code creates a large number of large objects, and for a long time can not be collected by the garbage collector (there is cited).

Java堆从GC的角度还可以细分为:新生代(Eden区、From Survivor区和To Survivor区) 和老年代。

Here Insert Picture Description

MinorGC的过程(复制-》清空-》互换):

  1. eden、SurvivorFrom复制到SurvivorTo,年龄+1
    首先,当Eden区满的时候会触发第一次GC,把还活着的对象拷贝到SurvivorFrom区,当Eden区再次出发GC的时候会扫描Eden区和From区域,对这两个区域进行垃圾回收,经过这次回收后还存货的对象,则直接复制到To区域(如果有对象年龄已经达到了老年的标准,则复制到老年代),同时把这些对象的年龄+1.

  2. 清空eden、SurvivorFrom:
    然后,清空Eden和SurivorFrom中的对象,也即复制之后有交换,谁空谁是to。

  3. SurvivorTo和SurvivorFrom互换:
    随后,SurvivorTo和SurvivorFrom互换,原SurvivorTo成为下一次GC时的SurvivorFrom区。部分对象会在From和To区域中复制来复制区,如此交换15次(由JVM参数MaxTenuringThreshold决定,这个参数默认为15),最终如果还是存活,就存入到老年代。

HotSpot内存管理

分代管理:
Here Insert Picture Description

因为:不同对象的生命周期不同,98%的对象是临时对象。

实际而言,方法区(Method Area)和堆一样,是各个线程共享的内存区域,它用于存储虚拟机加载的:类信息+普通常量+静态常量+编译器编译后的代码等等,虽然JVM规范将方法区描述为堆的一个逻辑部分,但它却还有一个别名叫Non-Heap(非堆),目的就是要和堆分开。

For the HotSpot virtual machine, many developers accustomed to the method area called "perpetual generations" (Parmanent Gen), but strictly speaking different both in nature, or permanent generations to implement the method area only, on behalf of a permanent method area ( is equivalent to an interface interface) of a realization, jdk1.7 version, the original has been placed in the permanent generation string constant pool removed.

Here Insert Picture Description

Permanent zone (before java7):

Persistent storage area is a permanent memory, for storing JDK itself carried by Class, metadata Interface, which means it is a block to be stored class information, is loaded into the data in this area will not be garbage collected away, closed the JVM will release the memory occupied this area.

Published 665 original articles · won praise 1894 · Views 240,000 +

Guess you like

Origin blog.csdn.net/cold___play/article/details/104076129