JVM memory structure

The public part

Thread sharing includes the Java heap, method area, and constant pool

The heap

The Java heap is an area of the JVM dedicated to memory configuration for Java instance objects, where almost all instance objects are allocated memory. Sometimes small objects are allocated directly on the stack.

The young and the old

The Java heap is divided into Old and Young, and Young is further divided into Eden, From Survivor 0, and To Survivor 1.

An object is allocated first in Eden of Young. When Eden runs out of memory, THE JVM starts GC. At this time, the objects that are not referenced in Eden will be reclaimed, while some objects with a long lifetime will be moved to Old. There is a parameter in the JVM called -xx :MaxTenuringThreshold that sets the number of GCS that are required to advance to the old age, that is, objects of the younger generation will enter the old age at the next GC after they have passed a specified number of GCS.

Java Heap regionalization Meaning Objects in a VM must have a long lifetime and a short lifetime. If you mix them together, you can expect more frequent GC because of the number of short-lived objects. GC scans all memory, but there are some objects that live too long to waste time scanning. For efficient garbage collection, partitioning is a no-no.

Default VM configuration: Eden:from:to = 8:1:1

Methods area

A method area is an area where Java class bytecode data is stored. It stores information for each class, such as runtime constant pools, field and method data, constructors, and so on. Constant pools are stored in the method area, but the Java Virtual Machine Specification places constant pools and method areas at the same level.

The method area is represented in different versions of virtual machines, for example in Hotspot 1.7, the method area is called Perm Space, while in 1.8 it is called MetaSpace.

Constant pool

Stored in the method area.

Private parts

The Java heap and method area data are shared, but some parts are thread private. The thread private part can be divided into PC register, Java virtual machine stack and local method stack.

PC register

The PC register, as the name implies, the Program Counter register, refers to the method that the thread is currently executing. If this method is not native, the PC register holds the address of the bytecode instructions being executed by the Java virtual machine. If the method is native, the PC register holds undefined. At any given time, a Java virtual machine thread executes code for only one method, and the method being executed by the thread is called the current method of the thread, whose address is stored in a PC register.

Java virtual machine stack

A virtual machine stack is created at the same time as a thread to store stack frames, which store local variables and some procedure results. Stack frame stores data including: local variable table, operand stack.

Local method stack

Support native methods

conclusion

A Java file is loaded into memory, and Java class information is stored in the method area. If you create an object, the object data is stored in the Java heap. If you call a method, you use PC registers, Java virtual machine stacks, local method stacks, and so on.