Talk about PermGen and Metaspace of jvm



This article focuses on the jvm’s PermGen and Metaspace

Java memory structure

Generation concept

For garbage collection algorithms, generational recycling is one of the advanced algorithms. Objects are divided into generations according to the generation time. Young objects that have just been generated are classified as young gen-generation, while objects that have survived for a long time are classified as Old generation. Depending on the specific implementation, more generations may be divided. For example, some count permanent generations as one generation.

Memory partition

Java memory is mainly divided into heap memory and non-heap memory, and its calculation formula is as follows:

Max memory = [-Xmx] + [-XX:MaxPermSize] + number_of_threads * [-Xss]

  • Heap structure

According to generations, young-eden,young-survivor,old
Use -Xmn,-Xms,-Xmx to specify

  • Non-heap structure

Including metaspace, threadstacks, compiled nativecode, memoryallocated by nativecode.

-xx: permize or-xx: metacessize,-xss or -XX:ThreadStackSize

PermGen and Metaspace

Change in string constant pool

  • In java7, the string constant pool was moved to java heap.

All strings that are interned are stored in the PermGen area. PermGen uses -XX:MaxPermSize=N to set the maximum size, but since the application String. intern is usually unpredictable and uncontrollable, it is not easy to set this size. If the setting is not good, it will often cause

java.lang.OutOfMemoryError: PermGen space
  • Java7 7,8 String Constant Pool Implemented in Heap

The string constant pool is limited to the heap memory of the entire application. calling () at runtime to increase the string constant will not make OOM permanent.

Changes in method areas

  • In java8, PermGen was removed and the method area was moved to Metaspace in non-heap.

move name and fields of the class, methods of a class with the bytecode
of the methods, constant pool, JIT optimizations etc to metaspace

  • Metaspace belongs to non-heap

The biggest difference between Metaspace and PermGen is that Metaspace is not in the virtual machine, but uses local memory.

If-xx: maxmetaaspacesize is not used to set the metadata size of the class, its maximum available space is the available space in the entire system memory. JVM can also increase local memory space to meet the storage of class metadata information.
However, if the maximum value is not set, there may be a bug that causes the space of Metaspace to expand ceaselessly, resulting in insufficient memory on the machine. The swap memory may be exhausted. Finally, the process is directly kill by the system.

  • OOM exception

If the space occupation of class metadata reaches the value set by MaxMetaspaceSize, garbage collection of objects and class loaders will be triggered.

java.lang.OutOfMemoryError: Metaspace space

The JVM throws from Metaspace after capturing memory allocation failures one by one.

Metaspace related parameters

  • -xx: metaparacesize, initial space size, reaching this value will trigger garbage collection for type unloading, and GC will adjust this value at the same time: if a large amount of space is released, reduce this value appropriately; If a small amount of space is released, the value should be appropriately increased when the MaxMetaspaceSize is not exceeded.
  • -XX:MaxMetaspaceSize, the maximum space, is unlimited by default.
  • -xx: MinMetaspacefreeratio, after GC, the minimum percentage of metaaspace’s remaining space capacity reduces garbage collection caused by allocating space
  • -xx: maxMetaspacefreeratio, after GC, the percentage of the maximum metaaspace remaining space capacity, reducing garbage collection caused by free space


Stripping constant pool from PermGen to heap and metadata from PermGen to metadata area, the benefits of removing PermGen are as follows:

  • The string constant pool is separated from PermGen and from the class metadata to enhance the independence of the class metadata.
  • Dividing metadata from PermGen to Metaspace can improve metadata management and GC efficiency.

Metadata in PermGen may move with each Full GC occurrence. Each type of garbage collector of HotSpot virtual machine requires special processing of metadata in PermGen. After separation, Full GC can be simplified and future concurrent isolation metadata can be optimized.

  • In preparation for the subsequent integration of HotSpot and JRockit.

PermGen is unique to the implementation of HotSpot, and JRockit did not mention PermGen.