Summary of JVM runtime data area

  • The JVM is briefly

  • Runtime data area

1. JVM memory model, which areas, the respective role

Methods area

The method area is used to store information about classes loaded by the Java virtual machine, constants, static variables, run-time constant pools, etc. Prior to JDK8, persistent generations were defined in the method area. The method area is described as a logical part of the heap because it is implemented using persistent generation. But it is “non-heap,” just that the collector in the design heap extends into the method area. In JDK8, the permanent generation was replaced with Metaspace. This is good for the following reasons

  • Metaspace uses local memory
  • String constant pool exists in persistent generation, which is prone to performance problems and memory overflow.
  • Information about classes and methods is difficult to determine, and it is difficult to specify the size of the permanent generation. If the size is too small, it is easy to operate the permanent generation overflow, and if the size is too small, it will cause the old generation overflow.
  • Persistent generation can introduce unnecessary complexity into the GC.

The Java heap

The Java heap is the largest chunk of memory in the JVM. It is used to store object instances and array values. You can think of all objects in Java created by New as memory allocated here, and the heap area is shared by all threads. As shown in the figure above, the heap is divided into many regions, Cenozoic, old and permanent. The Cenozoic was divided into one Eden region and two survivor regions. The Java heap is essentially an area of memory managed by the garbage collector (GC), partitioned to allow for more efficient garbage collection.

The virtual machine stack

The Java virtual machine stack is thread private, with one virtual machine stack for each thread. Each method execution creates a stack frame, which is the basic data structure of the method runtime. The main storage of the stack frame is local variable table, operand stack, dynamic link, method exit, etc. When the method is called, the stack frame is pushed into the JVM stack, and when the method is executed, the stack frame is pushed out of the stack. Two exceptions are defined in the virtual machine stack. If the stack depth of the thread call is greater than the maximum depth allowed by the virtual machine, a StatckOverFlowError is raised. Most Java virtual machines, however, allow dynamic expansion of the virtual stack size (a small percentage is fixed), so threads can apply for stacks until they run out of memory, at which point outofMemoryErrors are thrown.

Local method stack

The local method stack is similar to the virtual machine stack in that the JVM does not require the local method stack to implement method calls in any particular language. Used to support the execution of native methods, storing the state of each native method call. The local method stack and the VIRTUAL machine method stack run the same mechanism, the only difference is that the virtual machine stack is used to execute Java methods, while the local method stack is used to execute native methods. In many virtual machines, the local method stack and the virtual machine stack are used together.

Program counter

A program counter is a small area of memory, either a CPU register or operating system memory, that is used to indicate the line number of the bytecode executed by the current thread. It can be understood as a line number indicator of the current thread. The bytecode interpreter works by changing the value of this counter to fetch the next statement instruction. Each program counter is used only to record the line number of one thread, so it is thread private (one program counter per thread). If the program executes a Java method, the counter records the address of the virtual machine bytecode instruction being executed. If you are executing a native method, the counter value is Undefined. Since the program counter only records the current instruction address, there is no memory overflow. Therefore, The program counter is also the only area of any JVM memory region where OutOfMemoryError is not defined

2.Java8 memory generation improvements

There is no permanent generation. Type information, fields, methods, constants are stored in the local memory meta-space, but the string constant pool, static variables are still in the heap

The evolution details of the method area are clear first: only HotSpot has persistent generations. For BEA JRockit, IBM J9, etc., there is no concept of permanent generation. In principle, the implementation method area belongs to the implementation details of virtual machines and is not governed by the Java Virtual Machine Specification and does not require uniformity. Method area change in Hotspot: JDk16.And before: Permanent generation, static variables stored in permanent generation jdk17.: permanent generation exists, but has been gradually "depermanent generation", string constant pool, static variables removed, stored in heap JDk18.And after: no persistent generation, type information, fields, methods, constants are kept in the local memory meta space, but the string constant pool, static variables are still in the heap for what is to be replaced by the meta space. With the advent of Java8, persistent generation is no longer seen in HotSpot VM. But that doesn't mean classes. The metadata information of the. This data is moved to an area of local memory not connected to the heap, called Metaspace. Since the metadata of a class is allocated in local memory, the maximum allocatable space of the metadata space is the memory space available to the system. This change was necessary for the following reasons:1Setting the size of the space for the permanent generation is difficult to determine. In some scenarios, if there are too many dynamically loaded classes, O0M in the Perm area is easily generated. For example, in a practical Web project, because there are many function points, many classes need to be dynamically loaded during operation, and fatal errors often occur."The Exception in the thread 'dubbo client 7.0.x.x connector' Java lang. OutOfMemoryError: PermGenspace"The biggest difference between a meta-space and a permanent generation is that the == meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta-space is limited only by local memory ==.2Tuning persistent generations is difficult. Jdk7 puts StringTable into heap space. Because the collection efficiency of the permanent generation is low, it is triggered during full GC. Full GC is triggered when the old generation runs out of space and the permanent generation runs out. This results in inefficient StringTable recycling. However, in our development, a large number of strings will be created, and the recycling efficiency is low, resulting in insufficient memory of permanent generation. Put it in the heap, it can recycle memory in time.Copy the code

3. When do Jvm runtime data area objects enter the old area

  • Object age reaches old age standard, default is 15

  • Eden still does not have enough space to store large objects (size between 2KB and 128KB) after passing through MinorGC, which directly enters the old age area

    newPlay the Eden zone first. This area has a size limit. When Eden fills up and the program needs to create objects again, the JVM's garbage collector will perform a Minor GC on Eden garden, destroying objects in Eden garden that are no longer referenced by other objects. Load new objects into the Garden of Eden and move the remaining objects from the garden of Eden to the survivor0Area. If garbage collection is triggered again, the last surviving item is put into the survivor0If it's not recycled, it goes to the survivors1Area. If the garbage collection is repeated, the survivor will be put back0Zone, then go to the survivors1Area. When can WE go to the nursing home? You can set the number of times. The default is15Times. · Parameters can be set: -xx :MaxTenuringThreshold= for setting. In the retirement area, relatively leisurely. When the memory of the elderly area is insufficient, GC: Major GC is triggered again to clean the memory of the elderly area. An OOM exception will be generated if the endowment area fails to save objects after performing Major GCCopy the code

4. Why is there a New generation and an old generation? Why are the new generation Eden and Survivor? Why have two survivor zones?

The main purpose is to optimize GC performance. Different objects have different life cycles. Most objects are temporary objects.

  • Because some objects have long life and some objects have short life. You should place long-lived objects in one zone and short-lived objects in one zone. Different districts use different garbage collection algorithms. The cleaning frequency of the area with short life is higher, and that of the area with long life is lower. Improve efficiency.

If there is no Survivor zone, then every time Eden is Full, the surviving objects are migrated to the old zone. When the old zone is Full, Full GC will be triggered. Full GC is very time-consuming.

  • Increase the memory of the old age, so the old age cleaning frequency is reduced, but the cleaning time is longer.

  • Reduce old age memory, old age a FullGC less time, frequency increased.

None, just add another layer of Survivor. Add the objects in Eden area that are full to Survivor area, and then put them in the old area after repeatedly cleaning for several times. In this way, the pressure in the old area will be much lower. In other words, Survivor is like a sieve, which screens out the ones with short life cycle and puts the ones with long life cycle into the old age area to reduce the number of times the old age is cleaned

Let’s take a look at oneTo clean up the memory, it is easy to generate memory fragmentation. In order to avoid memory fragmentation, I used the replication algorithm to neatly put the objects that survived the Eden area and Survivor area into an empty memory. Because the life cycle is generally short, the efficiency of the replication algorithm is relatively high in the case of few living objects.

Copy algorithm! [insert picture description here] (img – blog. Csdnimg. Cn / 20200401143…

Summary of JVM runtime data area

  • The JVM is briefly

  • Runtime data area

1. JVM memory model, which areas, the respective role

Methods area

The method area is used to store information about classes loaded by the Java virtual machine, constants, static variables, run-time constant pools, etc. Prior to JDK8, persistent generations were defined in the method area. The method area is described as a logical part of the heap because it is implemented using persistent generation. But it is “non-heap,” just that the collector in the design heap extends into the method area. In JDK8, the permanent generation was replaced with Metaspace. This is good for the following reasons

  • Metaspace uses local memory
  • String constant pool exists in persistent generation, which is prone to performance problems and memory overflow.
  • Information about classes and methods is difficult to determine, and it is difficult to specify the size of the permanent generation. If the size is too small, it is easy to operate the permanent generation overflow, and if the size is too small, it will cause the old generation overflow.
  • Persistent generation can introduce unnecessary complexity into the GC.

The Java heap

The Java heap is the largest chunk of memory in the JVM. It is used to store object instances and array values. You can think of all objects in Java created by New as memory allocated here, and the heap area is shared by all threads. As shown in the figure above, the heap is divided into many regions, Cenozoic, old and permanent. The Cenozoic was divided into one Eden region and two survivor regions. The Java heap is essentially an area of memory managed by the garbage collector (GC), partitioned to allow for more efficient garbage collection.

The virtual machine stack

The Java virtual machine stack is thread private, with one virtual machine stack for each thread. Each method execution creates a stack frame, which is the basic data structure of the method runtime. The main storage of the stack frame is local variable table, operand stack, dynamic link, method exit, etc. When the method is called, the stack frame is pushed into the JVM stack, and when the method is executed, the stack frame is pushed out of the stack. Two exceptions are defined in the virtual machine stack. If the stack depth of the thread call is greater than the maximum depth allowed by the virtual machine, a StatckOverFlowError is raised. Most Java virtual machines, however, allow dynamic expansion of the virtual stack size (a small percentage is fixed), so threads can apply for stacks until they run out of memory, at which point outofMemoryErrors are thrown.

Local method stack

The local method stack is similar to the virtual machine stack in that the JVM does not require the local method stack to implement method calls in any particular language. Used to support the execution of native methods, storing the state of each native method call. The local method stack and the VIRTUAL machine method stack run the same mechanism, the only difference is that the virtual machine stack is used to execute Java methods, while the local method stack is used to execute native methods. In many virtual machines, the local method stack and the virtual machine stack are used together.

Program counter

A program counter is a small area of memory, either a CPU register or operating system memory, that is used to indicate the line number of the bytecode executed by the current thread. It can be understood as a line number indicator of the current thread. The bytecode interpreter works by changing the value of this counter to fetch the next statement instruction. Each program counter is used only to record the line number of one thread, so it is thread private (one program counter per thread). If the program executes a Java method, the counter records the address of the virtual machine bytecode instruction being executed. If you are executing a native method, the counter value is Undefined. Since the program counter only records the current instruction address, there is no memory overflow. Therefore, The program counter is also the only area of any JVM memory region where OutOfMemoryError is not defined

2.Java8 memory generation improvements

There is no permanent generation. Type information, fields, methods, constants are stored in the local memory meta-space, but the string constant pool, static variables are still in the heap

The evolution details of the method area are clear first: only HotSpot has persistent generations. For BEA JRockit, IBM J9, etc., there is no concept of permanent generation. In principle, the implementation method area belongs to the implementation details of virtual machines and is not governed by the Java Virtual Machine Specification and does not require uniformity. Method area change in Hotspot: JDk16.And before: Permanent generation, static variables stored in permanent generation jdk17.: permanent generation exists, but has been gradually "depermanent generation", string constant pool, static variables removed, stored in heap JDk18.And after: no persistent generation, type information, fields, methods, constants are kept in the local memory meta space, but the string constant pool, static variables are still in the heap for what is to be replaced by the meta space. With the advent of Java8, persistent generation is no longer seen in HotSpot VM. But that doesn't mean classes. The metadata information of the. This data is moved to an area of local memory not connected to the heap, called Metaspace. Since the metadata of a class is allocated in local memory, the maximum allocatable space of the metadata space is the memory space available to the system. This change was necessary for the following reasons:1Setting the size of the space for the permanent generation is difficult to determine. In some scenarios, if there are too many dynamically loaded classes, O0M in the Perm area is easily generated. For example, in a practical Web project, because there are many function points, many classes need to be dynamically loaded during operation, and fatal errors often occur."The Exception in the thread 'dubbo client 7.0.x.x connector' Java lang. OutOfMemoryError: PermGenspace"The biggest difference between a meta-space and a permanent generation is that the == meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta-space is limited only by local memory ==.2Tuning persistent generations is difficult. Jdk7 puts StringTable into heap space. Because the collection efficiency of the permanent generation is low, it is triggered during full GC. Full GC is triggered when the old generation runs out of space and the permanent generation runs out. This results in inefficient StringTable recycling. However, in our development, a large number of strings will be created, and the recycling efficiency is low, resulting in insufficient memory of permanent generation. Put it in the heap, it can recycle memory in time.Copy the code

3. When do Jvm runtime data area objects enter the old area

  • Object age reaches old age standard, default is 15

  • Eden still does not have enough space to store large objects (size between 2KB and 128KB) after passing through MinorGC, which directly enters the old age area

    newPlay the Eden zone first. This area has a size limit. When Eden fills up and the program needs to create objects again, the JVM's garbage collector will perform a Minor GC on Eden garden, destroying objects in Eden garden that are no longer referenced by other objects. Load new objects into the Garden of Eden and move the remaining objects from the garden of Eden to the survivor0Area. If garbage collection is triggered again, the last surviving item is put into the survivor0If it's not recycled, it goes to the survivors1Area. If the garbage collection is repeated, the survivor will be put back0Zone, then go to the survivors1Area. When can WE go to the nursing home? You can set the number of times. The default is15Times. · Parameters can be set: -xx :MaxTenuringThreshold= for setting. In the retirement area, relatively leisurely. When the memory of the elderly area is insufficient, GC: Major GC is triggered again to clean the memory of the elderly area. An OOM exception will be generated if the endowment area fails to save objects after performing Major GCCopy the code

4. Why is there a New generation and an old generation? Why are the new generation Eden and Survivor? Why have two survivor zones?

The main purpose is to optimize GC performance. Different objects have different life cycles. Most objects are temporary objects.

  • Because some objects have long life and some objects have short life. You should place long-lived objects in one zone and short-lived objects in one zone. Different districts use different garbage collection algorithms. The cleaning frequency of the area with short life is higher, and that of the area with long life is lower. Improve efficiency.

If there is no Survivor zone, then every time Eden is Full, the surviving objects are migrated to the old zone. When the old zone is Full, Full GC will be triggered. Full GC is very time-consuming.

  • Increase the memory of the old age, so the old age cleaning frequency is reduced, but the cleaning time is longer.

  • Reduce old age memory, old age a FullGC less time, frequency increased.

None, just add another layer of Survivor. Add the objects in Eden area that are full to Survivor area, and then put them in the old area after repeatedly cleaning for several times. In this way, the pressure in the old area will be much lower. In other words, Survivor is like a sieve, which screens out the ones with short life cycle and puts the ones with long life cycle into the old age area to reduce the number of times the old age is cleaned

Let’s take a look at oneTo clean up the memory, it is easy to generate memory fragmentation. In order to avoid memory fragmentation, I used the replication algorithm to neatly put the objects that survived the Eden area and Survivor area into an empty memory. Because the life cycle is generally short, the efficiency of the replication algorithm is relatively high in the case of few living objects.

Replication algorithmThis requires one empty memory, and if I have three extents, I can always keep one empty, so that when I clean out the garbage, I can put all the living objects neatly into one empty memory without fragmentation.

5. The difference between heap and stack? The structure of the heap?

Java divides memory into stack memory and heap memory. The main differences between the two are:

  • The stack is thread private and the heap is an area of memory shared by threads

  • Purpose: The virtual machine stack is used to describe Java execution, and the heap is used to store objects and data generated during execution

  • Collection: stack only on and off the stack no collection mechanism, the heap has a garbage collection mechanism

Heap structure:

  • Before JDK 7: New area + endowment area + permanent area (logically, fact not heap)

  • After JDK 8: new area + endowment area + meta space

6. Does garbage collection occur in Jvm permanent generations?

Yes, the Java Virtual Machine Specification is very relaxed about method areas, mentioning that virtual machines are not required to implement garbage collection in method areas. In fact, collectors do exist that do not implement or do not fully implement method area type offloading (for example, the 2GC collector in the JDK11 era did not support class offloading). Generally speaking, the recovery effect of this area is not satisfactory, especially for the type of unloading, the conditions are quite harsh. But recycling in this area is sometimes necessary. Several of the most serious bugs on Sun’s previous Bug list were memory leaks caused by earlier versions of the Hotspot VIRTUAL machine not fully reclaiming this area. The method area garbage collection collects two main parts: obsolete constants in the constant pool and types that are no longer used