preface

2021.04.30. I am very happy to have been in the new company for one and a half months. I’m going to put together a few articles about the JVM and GC. Now it is inevitable that the writing style is immature and rough, and the notes are also expressed in the language that they can understand for their own summary and combing. If I am lucky enough to be seen by the big guy, I really hope that you can point out my shortcomings and shortcomings, and guide me in what direction I should study and think more deeply. Nuggets — a community that helps developers grow and wants to grow from small fry to small fry.

Program counter

Line number indicator of bytecode executed by the current thread. Can change the value of the counter to get the next bytecode instruction to be executed. Each thread does not affect each other and is stored independently. This memory area is thread private. If the thread executes a non-native method, the program counter stores the address of the current instruction. If the native method is executed, the value in the program counter is undefined. The amount of space occupied by data stored in the program counter does not change from program to program, and there are no OutofMemoryErrors.

Local method stack

Native methods used by virtual machines. StackOverFlowError and OutOfMemoryError are thrown.

Java virtual machine stack

The Java stack is an in-memory model for running Java methods. It holds stack frames, which are created each time a method is called: a local variable table, a stack of operands, a reference to the run-time constant pool of the class to which the currently running method belongs, a method return address, and some additional information. This is also private to the thread. StackOverFlowError and OutOfMemoryError are thrown.

  • Local variable scale, used to store local variables.
  • Operand stack, the calculation process of the program is done by it.
  • A reference to a run-time constant pool. Because constants from a class may be needed during method execution, a reference to a run-time constant is required. Used to hold various literal and symbolic references generated during compilation. Dynamic
  • Method returns address. When a method is finished executing, it returns to where it was called before.

Stack frames are data structures used to store data and partial procedure results. They are also used to handle dynamic linking, method return values, and exception dispatch.

When a thread executes a method, it creates a corresponding stack frame and pushes the stack frame. When the method completes execution, the stack frame is pushed off the stack. Therefore, the stack frame corresponding to the method currently executed by the thread must be at the top of the Java stack. Two anomalies are specified in this area:

If the thread requests a stack deeper than the virtual machine allows, a StackOverFlowError is raised! If the virtual stack can be extended dynamically, OutOfMemoryError will be raised when it is extended to the point where it cannot allocate enough memory!

The Java heap

The JVM manages the largest area of memory, and the heap is intended to hold instances of objects. It is also the main administrative area for the garbage collector. An area shared by threads. OutOfMemoryError is thrown. When there are no instances of memory allocation and the heap can no longer expand. Garbage collection, the heap is divided into: young generation: Eden, To Survivor, From Survivor 8:1:1 old generation permanent generation (jdk8 has been removed)

Method region (permanent generation)

The logical part of the heap. Contents:

  1. Class information: class name/inheritance/attributes of the class itself
  2. Method information: loaded with the class, method name/method properties/related exception information, etc
  3. Constant pool
  4. Class variables: that is, non-final class variables
  5. Instance references to java.lang.class: Each loaded class builds an instance of java.lang.class, whose references are stored in the method area
  6. Method table: convenient and quick activation method
  7. A reference to classLoader

It can be seen that most of the data in the method area is loaded with the class load, basically the metadata of the class, often no change after loading. Is there GC in the method area? The answer is yes, but can you see that the size of the method area of class data is fixed even if there is no instance of the class, except that the class variable may change and need to be gc? The default method area size is 20.75 MB. You can manually change the method area size:

-xx :MaxPermSize # Set the maximum space that can be allocated for the permanent generationCopy the code

OutOfMemoryError is thrown when the memory allocation cannot be satisfied.

In Java8, method areas exist in Metaspace. The meta-space is no longer contiguous to the heap and exists in local memory.

By default, the use of local memory is unlimited, but the JVM also provides some parameters to limit the use of the meta space: -xx :MetaspaceSize # MetaspaceSize # MetaspaceSize # MetaspaceSize # MetaspaceSize # MetaspaceSize -xx :MinMetaspaceFreeRatio #GC Minimum remaining percentage of the post-meta space. -xx :MaxMetaspaceFreeRatio #GC Maximum remaining percentage of the post-meta space

Run-time constant pool

Is the part of the method area that holds the various literal and symbolic references generated during compilation. Dynamic

Direct memory

Being used frequently also causes OutOfMemoryError JDK1.4 to introduce a NIO mechanism. Based on Channel and Buffer I/O mode, the Native function library is used to allocate the out-of-heap memory directly, and then the DirectByteBuffer object is used as the reference of this memory for operation

  • Why abolish permanent generation
  1. To merge HOTPOTJVM and JRokit VM
  2. Strings exist in persistent generation, which is prone to performance problems and memory overflow.
  3. It is difficult to determine the size of information about classes and methods, so it is difficult to specify the size of permanent generation. If the size is too small, it is easy to overflow the permanent generation, while if the size is too large, it is easy to overflow the old generation.
  4. Persistent generation introduces unnecessary complexity to the GC and is inefficient for collection.