JVM learning summary (1) runtime data area

I bought the book "In-depth Java Virtual Machine" for a while. At that time, I just read it, and I didn't summarize it while reading it. Finally, I found that there was not much left in my mind. Now start to learn again in a good summary.

runtime data area

In the process of executing a Java program, the JVM divides the memory it manages into several different data areas. The managed memory includes the following runtime data areas

Enter image description

  1. program counter
  • Function: Record the line number of the bytecode executed by the current thread. When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter.
  • Meaning: JVM multi-threading is achieved by switching threads in turn and allocating CPU time. Therefore, in order to restore to the correct specified position after thread switching, each thread lost requires an independent program counter
  • Storage content: If the thread is executing a Java method, the program counter records the address of the virtual machine bytecode instruction of the executing thread. If the thread is executing a native method, the value in the program counter is null
  • Possible exceptions: The program counter is the only area where the Java Virtual Machine specification does not specify any OutOfMemoryError conditions
  1. Java virtual machine stack
  • Function: Describe the memory model of Java method execution. When each method is executed, a stack frame is created. The stack frame stores information such as local variable table, operand stack, dynamic link, method exit, etc.
  • Meaning: The method from the beginning of execution to the end of execution corresponds to the process of a stack frame being pushed to the stack in the Java virtual machine stack.
  • Storage content: Local variable table (various basic data types, reference types and returnAddress type that point to a bytecode instruction known during compilation), operand stack, dynamic link, method exit, etc. It is worth noting: local variable table The required memory space is allocated during compilation. The size of the local variable table is not changed during method execution
  • Possible exceptions: StackOverflowError: If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, this exception will be thrown OutOfMemoryError: If the virtual machine stack can be dynamically expanded (most of the current Java virtual machines can be dynamically expanded, It's just that the virtual machine specification also allows a fixed-length virtual machine stack) If enough memory cannot be applied for during expansion, this exception will be thrown
  1. native method stack
  • Function: serve the Native method called by the JVM ps: The Sun HotSpot virtual machine directly combines the local method stack and the virtual machine stack into one
  • Possible exceptions: the same as the virtual machine stack, there are StackOverflowError exceptions and OutOfMemoryError exceptions
  1. Java heap
  • Role: Allocate all object instances and arrays, but with the development of just-in-time compilers (JIT) and the gradual maturity of escape analysis technology, on-stack allocation and scalar replacement optimization techniques make the allocation of objects on the pair less absolute.
  • Meaning: The Java heap is the largest piece of memory managed by the Java virtual machine, shared by all threads, and created when the JVM starts. The Java heap is the main area managed by the garbage collector. The Java heap can be subdivided into: Young Generation, Old Generation, and Permanent Generation; the new generation can be divided into Eden space, From Survivor space, To Survivor space. If you configure -XX:+/-UseTLAB, each thread pre-allocates a small piece of memory in the Java heap, called the Thread Local Allocation Buffer (TLAB)
  • Storage content: Store object instances and arrays (when creating an array, the creation action is triggered by the bytecode instruction newarray, and the virtual machine automatically generates a class that is a subclass of java. Properties and methods, which can be directly accessed are the length property of the public type and the clone() method), where almost all object instances are allocated. The heap can be in a physically discontinuous memory space, as long as it is logically contiguous
  • Possible exceptions: If there is no memory in the heap to complete the instance allocation, and the heap can no longer be expanded (the heap can be fixed in size, and the expansion can be controlled by the -Xms and -Xmx parameters), an OutOfMemoryError exception will be thrown
  1. method area
  • Function: used to store data such as runtime constant pool, class information that has been loaded by virtual machine, constants, static variables, code compiled by just-in-time compiler
  • Significance: Provisions are made for data such as runtime constant pools, constants, static variables, etc.
  • Storage content: runtime constant pool (dynamic), class information that has been loaded by the virtual machine, constants, static variables, code compiled by the just-in-time compiler, etc.
  • Possible exceptions: Throw OutOfMemoryError exception when the method area cannot meet the memory allocation requirements

runtime constant pool

The runtime constant pool is part of the method area. In the Class file, in addition to the description information of the class version, field, method, interface, etc., there is also a constant pool, which is used to store various literals and symbolic references generated during compilation. This part of the content will be loaded in the class. Then enter the runtime constant pool of the method area to store

jdk6, jdk7, jdk8 have different designs for the method area

  • The runtime constant pool is part of the method area in JDK1.6 and earlier versions of the JVM, while the method area is placed in the "permanent generation" in the HotSpot virtual machine. So the runtime constant pool is also in the permanent generation
  • However, the JVM of JDK1.7 and later versions has moved the runtime constant pool from the method area, and opened up an area in the Java heap (Heap) to store the runtime constant pool.
  • In JDK1.8, there is no permanent generation at all, and the method area is directly placed in a local memory area that is not connected to the heap. This area is called metaspace.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324992279&siteId=291194637