Java Heap Memory is the area in a Java virtual machine (JVM) that stores object instances. To manage and recycle memory more efficiently, the Java heap is usually divided into different regions, each with specific uses and characteristics. The following are the definition, function and related examples of Java heap memory partition.
Java heap memory partition
The Java heap is usually divided into three major regions:
- Young Generation
- Old Generation
- Permanent Generation / Metaspace
1. Young Generation
definition:Young generations are used to store newly created objects. This generation is further divided into three areas: Eden District, Survivor From District and Survivor To District.
effect:
- Eden District: Most newly created objects first allocate memory in the Eden area.
- Survivor From and Survivor To areas: Used for copying of surviving objects. During garbage collection, the surviving objects are copied from one Survivor area to another Survivor area or moved to the elderly.
Garbage recycling:The younger generation adopts Minor GC (small garbage collection). Since most objects become unreachable very quickly after creation, Minor GC can quickly recycle these objects.
Example:
public class YoungGenerationExample { public static void main(String[] args) { for (int i = 0; i < 1000; i++) { // Create short life cycle objects and allocate them in the Eden area byte[] array = new byte[1024 * 1024]; } } }
In this example, a large number of short life cycle objects are created and allocated in the Eden area.
2. Old Generation
definition:Older generations are used to store objects with longer life cycles. The objects that still survive after several times Minor GC will be moved to the old age.
effect:The elderly are used to store objects with a longer life cycle, which are not easily recycled.
Garbage recycling:The older generation uses Major GC (also known as Full GC). Due to the high survival rate of older subjects, Major GCs are less frequent but take longer.
Example:
public class OldGenerationExample { public static void main(String[] args) { // Create objects with longer life cycles byte[] array = new byte[10 * 1024 * 1024]; // Keep the object alive and prevent it from being recycled while (true) { try { (1000); } catch (InterruptedException e) { (); } } } }
In this example, a large object with a longer life cycle is created and will eventually be moved to the old age.
3. Permanent Generation and Metaspace
definition:Permanent generation and metaspace are used to store class metadata, constant pools, static variables, and JIT compiled code.
- Permanent Generation: Used to store class metadata and other class-related structures. Prior to Java 8, the permanent generation was the implementation of the method area.
- Metaspace: Starting from Java 8, the permanent generation was replaced by metaspace. Metaspace uses native memory instead of heap memory.
effect:
- Permanent generation: Store class information, constants, static variables and method data.
- Metaspace: More flexible management of class metadata, avoiding the fixed size limitation of permanent generations.
Example:
public class MetaspaceExample { public static void main(String[] args) { // Create a large number of class loading operations to increase the use of metaspace for (int i = 0; i < 10000; i++) { ClassLoader loader = new DynamicClassLoader(); try { Class<?> clazz = ("" + i); Object instance = (); } catch (Exception e) { (); } } } } class DynamicClassLoader extends ClassLoader { @Override public Class<?> findClass(String name) throws ClassNotFoundException { byte[] b = loadClassData(name); return defineClass(name, b, 0, ); } private byte[] loadClassData(String name) { // Dynamically generate bytecode return new byte[0]; } }
In this example, a large number of classes are created through a dynamic class loader to increase the use of metaspace.
Summarize
- Young Generation: Stores newly created objects, including the Eden area and two Survivor areas, and performs Minor GC.
- Older age: Store objects with a long life cycle and perform Major GC.
- Permanent generation / metaspace: Store class metadata. After Java 8, metaspace is used, avoiding the fixed size limitation of permanent generations.
Understanding Java heap memory partitioning helps optimize application performance and effectively manage memory resources.
This is the end of this article about the implementation example of Java heap memory partition. For more related content on Java heap memory partition, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!