JVM heap and method area

Table of contents

1. Heap

1.1 The structure of the heap

1.1.1 Young Generation

1.1.2 Old Generation (Old Generation)

1.1.3 Permanent Generation/Metaspace

 1.2 Heap memory overflow

1.3 Heap memory diagnosis

1.3.1 jmap

1.3.2 jvisualvm

2. Method area

2.1 Structure of the method area


1. Heap

The JVM heap is the core of Java program runtime memory management, and it is mainly used to store object instances and arrays. Heap memory is characterized by dynamic allocation and recovery, which allows the creation and destruction of objects, but also requires attention to memory leaks and performance issues.

1.1 The structure of the heap

The JVM heap is usually divided into three main parts:

1.1.1 Young Generation

The new generation is used to store newly created objects. It is divided into three regions: Eden space and two Survivor spaces (often called From and To spaces). The newly created objects are first allocated to the Eden space, and after a garbage collection, the surviving objects will be moved to the Survivor space. After multiple cycles, the surviving objects are moved to the old generation.

1.1.2 Old Generation (Old Generation)

The old generation is used to store objects with a long life cycle. After multiple garbage collections in the young generation, surviving objects are promoted to the old generation. Objects in the old generation generally need to go through more garbage collection cycles before being recycled.

1.1.3 Permanent Generation/Metaspace

In earlier JVM versions, the permanent generation was used to store metadata, method information, and static variables of the class. However, since the permanent generation is prone to memory leaks and overflows, the JVM after 1.8 introduced the metaspace instead. Metaspace metadata is stored in local memory and is no longer limited by a fixed size.

jdk1.8 and later: In the heap memory, it exists logically but does not exist physically (metaspace uses local memory), as shown in the following figure:

What are PermGen and Metaspace?

The method area is a specification, and different virtual machine vendors can make different implementations based on the specification. The permanent generation and metaspace are based on the implementation of different jdk versions.
The method area is like an interface, and the permanent generation and metaspace are two different implementation classes.
It’s just that the permanent generation is the initial implementation class of this interface. Later, this interface has been changed until the implementation class is completely abandoned and replaced by a new implementation class—metaspace.

 1.2 Heap memory overflow

Use the following code:

public class a {
    public static void main(String[] args)  {
        List<String> list=new ArrayList<>();
        String a="hello";
        while (true){
            a=a+a;
            list.add(a);
        }
    }
}

After executing the above code, a heap memory overflow will occur as shown in the figure below:

The size of the Java heap can be configured through command line parameters, the main parameters include:

  • -Xms: Set the initial size of the heap.
  • -Xmx: Sets the maximum size of the heap.

Usually, setting these two parameters to the same value can reduce the dynamic adjustment of the heap and improve performance. For example:

java -Xms512m -Xmx512m -jar YourApp.jar

This will set both the initial and maximum size of the heap to 512 megabytes.

1.3 Heap memory diagnosis

1.3.1 jmap

First, jps finds the running process of java, and then jmap -heap process id can view the heap memory, as shown below:

1.3.2 jvisualvm

Execute the following code:

public class a {
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(30000);
        byte[] bytes = new byte[1024 * 1024 * 50];
        System.out.println("-------");
       Thread.sleep(30000);
        bytes=null;
        System.out.println("-------");
        Thread.sleep(30000);
    }
}

Then execute jvisualvm to get the following figure: when bytes=null and garbage collection, the memory usage is directly reduced by 50M. As shown below:

2. Method area

The Method Area is another important memory area of ​​the JVM, which is mainly used to store metadata, static variables, constant pools, and method codes of classes.

2.1 Structure of the method area

The method area contains the following main sections:

class metadata

The method area stores the metadata of each class, including class name, parent class, interface, field, method and other information. This information plays an important role when the program is running, such as method calls and field access.

 static variable

Static variables belong to the class, not to the instance of the object. These variables are initialized when the class is loaded and exist in the method area.

constant pool

The constant pool contains constants used in the class, such as strings, numbers, class names, etc. It provides storage for the runtime constants of the class.

method code

The method area stores the method code in the class, including bytecode instructions and bytecode representation of the method. These codes are executed when the method is called.

In earlier JVM versions, the method area was implemented as a permanent generation. However, due to memory leaks and performance issues with the permanent generation, the JVM introduced metaspace in newer versions to replace the permanent generation. The advantage of the metaspace is that it is no longer limited to a fixed size, which avoids some problems caused by the permanent generation. As shown below:

Guess you like

Origin blog.csdn.net/qq_43649937/article/details/132626704