Finally understand the memory structure of java8, no longer entangled in the method area and constant pool!

Reprinted from: Finally understand the memory structure of java8, no longer entangled in the method area and constant pool!

Java8 memory structure introduction

 The java virtual machine has changed a lot in jdk8. There are various explanations on the Internet. After consulting the official documents and the explanation of the big guys, I will briefly introduce the memory structure of java8 that I understand.

table of Contents

  1. Memory structure diagram summary
  2. The difference between virtual machine memory and local memory
  3. java runtime data area
    1. Program Counter Register
    2. Virtual machine stack (JVM Stacks)
    3. Native Method Stacks
    4. Java ((Java Heap)
    5. Method Area
  4. Direct memory
  5. common problem
    1. What is the Native method?
    2. Where are the member variables, local variables, and class variables stored in the memory?
    3. Where are the constants modified by final stored?
    4. What is the relationship between class constant pool, runtime constant pool, and string constant pool? What's the difference?
    5. What is literal quantity? What is a symbolic reference?

Java8 memory structure diagram

java memory structure


The difference between virtual machine memory and local memory

 When the Java virtual machine is executing, it will allocate the managed memory into different areas. These areas are called 虚拟机内存. At the same time, the physical memory that the virtual machine does not directly manage is also used to a certain extent. These are used but not in the virtual machine memory data The memory of the area, we call it 本地内存, there are certain differences between these two types of memory:

  • JVM memory
    • Controlled by the parameter of the virtual machine memory size, OOM will be reported when the size exceeds the size set by the parameter
  • Local memory
    • Local memory is not limited by virtual machine memory parameters, only limited by physical memory capacity
    • Although not limited by parameters, if the memory usage exceeds the size of physical memory, OOM will also be reported

java runtime data area

 During the execution of the java virtual machine, the managed memory is divided into different areas, some with the generation and disappearance of threads, and some with the generation and disappearance of the java process. According to the "Java Virtual Machine Specification", the runtime The data is divided into the following areas:

Program Counter Register

 The program counter is the line number indicator of the bytecode executed by the current thread. By changing the value of the counter, the next line of instructions is selected, and functions such as jumping, looping, and restoring threads can be realized through it.

  • At any time, a processor core can only run one thread. Multithreading is accomplished by switching between threads in turn and allocating time. This requires a flag to remember where each thread has been executed, and the program counter is needed here. .
  • So, the program counter is yes 线程私有, each thread has its own program counter.

Virtual machine stack (JVM Stacks)

Virtual machine stack

 The virtual machine stack is yes 线程私有, with threads coming and going. The virtual machine stack describes the memory model of methods in threads:

  • When each method is executed, a stack frame is synchronously created in the virtual machine stack
  • Each stack frame contains the following content
    • Local variable table
      • The local variable table stores the java basic data types (byte/boolean/char/int/long/double/float/short) and object references in the method (note: the basic data types here refer to the local variables in the method )
    • Operand stack
    • Dynamic link
    • Method return address
  • The method is pushed into the stack when it is executed, and popped out after the execution is complete

 The virtual machine stack may throw two exceptions:

  • If the stack depth requested by the thread is greater than the stack depth specified by the virtual machine StackOverFlowError, a stack overflow will be thrown
  • If the stack capacity of the virtual machine can be dynamically expanded, then OutOfMemoryErrorOOM memory overflow will be thrown when the virtual machine stack cannot apply for memory

Native Method Stacks

 The functions of the local method stack and the virtual machine stack are similar, both throw OutOfMemoryErrorand StackOverFlowError, both 线程私有, the main difference is:

  • The virtual machine stack executes the java method
  • The native method stack executes native methods ( what is a native method? )

Java ((Java Heap)

 The java heap is the largest piece of JVM memory. 线程共享It is 由垃圾收集器管理a memory area that mainly stores object instances. Of course, due to the development of the java virtual machine, there are many more things in the heap. Now the main ones are:

  • Object instance
    • Objects generated by class initialization
    • Arrays of basic data types are also object instances
  • String constant pool
    • The string constant pool was originally stored in the method area, and jdk7 began to be placed in the heap.
    • The string constant pool stores a direct reference to the string object, not a directly stored object, it is astring table
  • Static variable
    • A static variable is a variable modified by static, which is migrated from the method area to the heap in jdk7
  • Thread allocation buffer (Thread Local Allocation Buffer)
    • 线程私有, But does not affect the commonality of java heap
    • Increase the thread allocation buffer is to improve the efficiency of object allocation

The java heap can be either a fixed size or expandable (through parameters -Xmxand -Xmssettings). If the heap cannot be expanded or the memory cannot be allocated, it will report OOM

Method Area

 The method area is definitely the focus of all articles on the java memory structure on the Internet, because the realization of the method area has made a big innovation in java8, now let's discuss:

The method area is all 线程共享the memory. Before java8, it was placed in the JVM memory. It was implemented by the permanent generation. Due to the limitation of the JVM memory size parameter, the content of the permanent generation was removed in java8. The method area was 元空间(Meta Space)implemented and placed directly In the local memory, it is not restricted by the JVM parameters (of course, if the physical memory is full, the method area will also report OOM), and the 字符串常量池sum of the original method area 静态变量is transferred to the Java heap, and the method area and others The area is different in that the content of the method area during compilation and after the class is loaded is slightly different, but in general it is divided into two parts:

  • Class meta information (Klass)
    • Class classifier information during compilation into the method area, which basic information such as the place, including classes 版本, 字段, 方法, 接口and常量池表(Constant Pool Table)
    • 常量池表(Constant Pool Table)Stored during compilation of the class 字面量, 符号引用( what is a literal? What is a symbol reference? ), this information will be parsed into the runtime constant pool after the class is loaded
  • Runtime Constant Pool
    • The runtime constant pool is mainly stored in the literal and symbolic references that are resolved after the class is loaded, but more than these
    • The runtime constant pool is dynamic, and data can be added. The most common use is the intern() method of the String class

Direct memory

 Direct memory is located in local memory and is not part of JVM memory, but it will also report OOM when physical memory is exhausted, so I will also talk about it.

 In jdk1.4, the NIO (New Input/Putput) class is added, and a new IO method based on channel and buffer is introduced. It can use native functions to directly allocate off-heap memory, and then store The DirectByteBuffer object in the java heap is used as a reference to this memory for operation, which can be used in some scenarios 大大提高IO性能,避免了在java堆和native堆来回复制数据.


common problem

What is the Native method?

 Because java is a high-level language, far away from the bottom of the hardware, sometimes it is impossible to manipulate the resources at the bottom. Therefore, java adds nativekeywords, and nativethe method modified by keywords can be rewritten in other languages, so that we can write one The native method is then rewritten in C language to manipulate the underlying resources. Of course, the use of native methods will lead to low portability of the system, which requires attention.

Where are the member variables, local variables, and class variables stored in the memory?

  • Class variable
    • Class variables are modified with static modifiers, variables defined outside the method, generated and destroyed with the java process
    • Store static variables in the method area before java8, and in the heap when java8
  • Member variables
    • Member variables are defined in the class, but are not modified by the static modifier. As instances of the class are generated and destroyed, they are part of the class instance
    • Since it is a part of the instance, when the class is initialized, the direct reference or value is taken from the runtime constant pool and put into the heap together with the initialized object
  • Local variable
    • Local variables are variables defined in the methods of the class
    • When the method is called, it is placed in the stack frame of the virtual machine stack. After the method is executed, it is popped from the virtual machine stack, so it is stored in the virtual machine stack

Where are the constants modified by final stored?

finalThe keyword does not affect the location in the memory, please refer to the previous question for the specific location.

What is the relationship between class constant pool, runtime constant pool, and string constant pool? What's the difference?

 Both the class constant pool and the runtime constant pool are stored in the method area, and the string constant pool has been migrated from the method area to the java heap in jdk7.

 In the class compilation process, the class meta information will be placed in the method area. Part of the class meta information is the class constant pool, which mainly stores literals and symbol references, and part of the literal is text characters. When the class is loaded Resolve literal and symbolic references into direct references and store them in the runtime constant pool; for text characters, they will look up the string constant pool during parsing to find out the direct reference of the string object corresponding to this text character, and will directly References are stored in the runtime constant pool; the string constant pool stores references to string objects, not the string itself.

What is literal quantity? What is a symbolic reference?

  • Literal
    • Java code cannot be used to construct references during compilation. Literals are a representation of data during compilation:
    int a=1;//这个1便是字面量
    String b="iloveu";//iloveu便是字面量
    
    • 1
    • 2
  • Symbolic reference
    • Since the address of each class is not known during the compilation process, because this class may not be loaded yet, if you refer to another class in one class, then you have no way of knowing its memory address. What should we do? You can only use his class name as a symbol reference, and use this symbol reference to get his memory address after the class is loaded.
    • Example: I reference com.test.Quest in the com.demo.Solution class, then I will com.test.Queststore it as a symbol reference in the class constant pool, and after the class is loaded, I will take this reference to the method area to find the memory of this class address.

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/108140832