[In-depth understanding of JVM-Java memory area]

As we all know, the memory management of C and C++ is controlled by developers (what malloc, free, etc.), but in java this right is handed over to jvm (java virtual machine)Managed by the automatic memory management mechanism of the virtual machine. This has brought benefits to java programmers, that is, there is no need to manually delete/free for each new operation, so relatively speaking, memory overflow is not easy to occur, but once memory overflow occurs, how to use memory for the virtual machine If you don’t understand, it will be difficult to troubleshoot errors.

1. Runtime data area

When the java virtual machine executes a java program, it divides the memory it manages into several different data areas.

1. Diagram

Insert picture description here

jvm running data area map

Insert picture description here

2. Five runtime data areas
  • Program counter
  • java virtual machine stack
  • Native method stack
  • java heap
  • Method area

(1) Program counter

  • Small memory space

  • Memory is thread private

  • Can be seen as the current thread execution bytecode indicator

  • If the thread is executing a java method, this counter records the address of the bytecode instruction of the virtual machine being executed. If the native method is being executed, the value of this counter is empty (Undefinde). This memory area is the only area that does not specify any OutOfMemoryError conditions in the Java virtual machine specification.

  • In the conceptual model of the virtual machine, the bytecode indicator selects the next bytecode instruction to be executed by changing the value of the program counter. Basic functions such as branches, loops, jumps, exception handling, and thread replies all need to rely on this counter to complete.

(2) Java virtual machine stack

  • Thread private (life cycle is the same as thread)
  • The java virtual machine stack describesMemory model of java method execution(Functions and methods are executed in this memory area)
  • Each method will be executed at the same timeCreate a "stack frame", Each method from the call to the completion of the process corresponds to aStack frameinvirtual machineinPushToUnstackThe process is out of the stack. (Refer to the function call diagram below)
// 假设执行了如下两个方法,则函数调用图如下
     new Person().name()
                 .age();

Insert picture description here

Function call graph

review:

We often say that basic types and reference types are allocated on the stack, and when new objects are allocated, memory is allocated on the heap. In fact, this sentence is not rigorous.

reason:

When we are learning the basics of java, there are often people who divide the memory of java into: stack and heap. These two pieces. In fact, these two divisions are relatively rough. This division can only show that most programmers are most concerned about. The memory areas that are most closely related to object memory allocation are these two. In fact, what they call "stack" is the part of the local variable table in the virtual machine stack. The local variable table stores various data types that can be known at compile time: the common data types in the eight, reference types (may be a reference pointer to the starting address of the object, or it may be a handle to a representative object or other related to this object Related location), and return Address type (pointing to the address of a bytecode instruction).

correct:

The basic types and reference types are allocated on the stack (actually the local variable table). When a new object is created, the memory is allocated on the heap.

The virtual machine specification stipulates two abnormal conditions for this area:
1. The stack depth requested by the thread is greater than the depth allowed by the virtual machine. Jvm throws StackOverflowError.
2. If the stack of the virtual machine can be dynamically expanded, if enough cannot be applied for during expansion. Memory jvm will throw OOM
ps: Most current virtual machines can be dynamically expanded, but the virtual machine specification also allows fixed-length virtual machine stacks

Introduction to frame stack composition:

  • Local variable table

1. Main preservationFunction parameters,as well asLocal variable information
2. After the function call is over, the local variable table will be destroyed with the destruction of the stack frame to release space.
3. If the function has many parameters or many local variables are defined, the local variable table will expand, and each method call will take up more More stack space eventually leads to a reduction in the number of function calls in the stack space under certain conditions.
4. The method with fewer local variables under the same stack capacity can support deeper function calls and more calls.

  • Operand stack

1. Last in first out data structure, the maximum depth is determined by the compilation period.
2. The operand stack is empty when the stack frame is just created
3. The method is executingPerform arithmetic operationsOr call other methodsParameter passingWhen isThrough the operand stack
4. When performing method operations, the operand stack is used to store the constants or variables copied by the jvm from the local variable table for extraction, and the results are pushed onto the stack.
5. Also usedStore the parameters needed to call the methodas well asAccept method return value result.
ps: The local variable table stores the parameters of the method, and the operand stack stores the parameters needed when calling the method (refer to the chestnut below)

  • Dynamic link

1. Each stack frame contains a reference to the method that the stack frame belongs to in the runtime constant pool. This reference is held to support dynamic linking during method invocation.
2. A large number of classes are stored in the class file Symbol reference. The method call instruction in bytecode takes the symbol reference pointing to the method in the constant pool as the parameter. Some of these symbol references will be converted into direct references during the class loading phase or the first time they are used. This conversion is called static resolution. The other part will be converted into a direct reference during each run. This part is called dynamic linking.

  • Return address (method exit)

After a method is executed, there are only two methods to exit the current method:
1. When the execution encounters a return instruction, the return value will be passed to the upper method caller. This exit method is called the normal completion exit (Normal Method). Invocation Completion), generally speaking, the caller's PC counter can be used as the return address
2. When the execution encounters an exception, and the current method body is not processed, it will cause the method to exit. At this time, there is no return value, which is called an exception After completing the exit (Abrupt Method Invocation Completion), the return address must be determined by the exception handler table.
When the method returns, three operations may be performed:
1. Restore the local variable table and operand stack of the upper method
2. Push the return value (if any) into the operand stack of the caller's stack frame
3. Adjust the PC ( Program counter) The value of the counter points to an instruction after the method call instruction, etc.

ps: The stack frame is also called the process activity record, which is a data structure used by the compiler to implement process/function calls.

chestnut:


     // 调用方法时
     sum(10,20;// 10 ,20 存放在操作数栈内

     public int sum(int a,int b){
    
      // 方法参数a,b 存放在局部变量表中
        int s = a+b;// s变量存放在局部变量表
        return s // 方法返回值存在操作数栈中
    }

(3) Local method stack

1. The role played by the
virtual machine stack is very similar. 2. The virtual machine stack serves for the virtual machine to execute java methods (that is, bytecode, the java virtual machine executes .class bytecode files), while the local method stack is Native method services used by the virtual machine
3. In the JVM specification, the language, usage, and data structure used by the native method in the native method stack are not mandatory, and the virtual machine can implement it freely. In the HotSopt virtual machine, the local method stack and the Java stack are directly combined into one.
4. This area will also throw oom or StackOverflowError

(4) java heap

  • The largest piece of memory area managed by the java virtual machine
  • The area shared by all threads, created when the virtual machine starts
  • Used to store the instance of the object
  • Almost all object instances and their arrays are allocated on the heap
  • The main area managed by the garbage collector is therefore often referred to as the "GC" heap
  • Allocate a certain amount of memory in the heap to save the object instance. In fact, it only saves the attribute value (field value) of the object instance, the type of the attribute, the type tag of the object itself, etc., and does not save the method of the object (in the frame stack The form is stored in the Stack), and a certain amount of memory is allocated in the heap to save the object instance. After the object instance is allocated in the heap, a 4-byte Heap memory address needs to be saved in the stack to locate the position of the object instance in the heap so that it is easy to find the object instance.
  • The java heap is in a physically discontinuous memory space, as long as it is logically continuous.
  • currentMainstream virtual machineAllAccording to scalable implementation(Controlled by -Xmx and -Xms), if there is no memory in the heap to complete the instance allocation, and the heap can no longer be expanded, an OutOfMemoryError will be thrown.

1. From the perspective of garbage collectionThe heap is divided into two different regions: the young generation (Young) and the old generation (Old). The young generation (Young) is divided into three regions: Eden, From Survivor, and To Survivor.
The purpose of this division is to enable JVM to better manage objects in heap memory, including memory allocation and recovery. (Refer to the partition diagram of the lower heap)
2. From the perspective of memory allocation, The java heap shared by threads may be divided into multipleThread-private allocation buffer(Thread Local Allocation Buffer)

Insert picture description here

Heap specific division diagram

(5) Method area

  • Thread shared memory area
  • For storageLoaded by the virtual machineData such as class information, constants, static variables, and code compiled by the just-in-time compiler.
  • When the .class file of the hard disk is loaded, it will be loaded into the method area, and then the method members in the class are parsed and a class object is created in the method area. This information is stored in the class object, and there is only one copy of this object.
  • The method area is also called shared area, permanent generation (not real permanent generation), persistent zone, non-heap.
  • HotSpot virtual machine design teamChoose to extend the GC generational collection to the method area, or use permanent generation to implement the method area. For other virtual machines (such as BEA JRockit, IBM J9, etc.), there is no concept of permanent generation.
  • Using permanent code to implement method area is also disadvantageous and easy to encounter OOM (permanent code has -XX:MaxPermSize upper limit, I9 and IRockit will be fine as long as the upper limit of the process is not touched, for example, 4GB of 32-bit system will not cause problems)
  • The HotSpot virtual machine team began to abandon the permanent generation and gradually changed to Native Memory to implement the method area. In jdk1.7, the character constant pool originally placed in the permanent generation was removed.
  • The Java virtual machine specification has very loose restrictions on the method area. In addition to requiring no continuous memory space like the Java heap, and can choose a fixed size or expandable, you can also choose not to implement garbage collection. Relatively speaking, garbage collection is in this area. The occurrence is relatively rare, but there are also, the main goal of memory recovery in this areaFor the recovery of the constant pool,withType of uninstall
  • The oom will be thrown when the method area cannot meet the memory requirements

(6) Runtime constant pool

  • Runtime Constant Pool, itIs part of the method area
  • The composition of the Class file includes: the description of the version, fields, methods, interfaces and other information of the class,Constant Pool Table
  • Constant pool of class filesUsed to store various literals and symbol references generated during compilation. This part of the content will be stored in the class after loadingRuntime constant pool of method areain.
  • In addition to saving the symbol references described in the class file, the compiled direct references are also stored in the runtime constant pool.
  • Constant pool of class filesIt is mainly used to store two types of constants: Literal and Symbolic References. Literals are equivalent to the concept of constants at the Java language level, such as text strings, declared asfinal constant value, etc., Symbolic references belong to the concept of compilation principles, including the following three types of constants:

1. Fully qualified names of classes and interfaces (that is, package names. Class names)
2. Field names and descriptors
3. Method names and descriptors

Note: The conceptual difference between the
constant pool of the class file and the runtime constant pool of the method area: 1. The class file constant pool is a part of the class file structure.
2. The runtime constant pool is a part of the method area of ​​the jvm runtime memory area.
3. The information in the class file constant pool will be loaded into the runtime constant pool when the class is loaded.

Benefits of constant pool:

1. The benefits of
constant pool The constant pool is to avoid frequent creation and destruction of objects and affect system performance, which realizes the sharing of objects. For example, in the string constant pool, all string literals are put into a constant pool during compilation.
2. Expand the meaning of ==

  • The double equal sign is used between the basic data types, and their values ​​are compared.
  • The double equal sign is used between compound data types (classes), and the comparison is their storage address in memory.

Constant pool technology expansion

Most of the packaging classes of basic types in java implement constant pool technology, namely Byte, Short, Integer, Long, Character, Boolean. These 5 wrapper classes create corresponding types of cache data with values ​​[-128, 127] by default, but new objects will still be created beyond this range. Wrapping class for two types of floating-point numbersFloat,DoubleandThe constant pool technology is not implemented.

Integer's chestnuts (comments in the code are very important)

        Integer i1 = 40;//Java在编译的时候会直接将代码封装成Integer i1=Integer.valueOf(40),吧数值存入常量池,然后从常量池中取i1的数据。
        Integer i2 = 40;
        Integer i3 = 0;
        Integer i4 = new Integer(40);// new 时直接在堆上创建新的对象i4
        Integer i5 = new Integer(40);
        Integer i6 = new Integer(0);

        System.out.println("i1=i2   " + (i1 == i2));//t
        System.out.println("i1=i2+i3   " + (i1 == i2 + i3));//t
        System.out.println("i1=i4   " + (i1 == i4));//f
        System.out.println("i4=i5   " + (i4 == i5));//f
        System.out.println("i4=i5+i6   " + (i4 == i5 + i6));//t ,由于+号操作不适合对象(这里i5、i6),所以这里对象会进行自动拆箱再相加。
        System.out.println("40=i5+i6   " + (40 == i5 + i6));//t

String chestnuts (comments in the code are very important)

        String stra = "abcd"; // 对象存储在常量池中
        String strb = new String("abcd");// new 就是在堆中分配了新的地址
        System.out.println(stra==strb);//false
       
        String str1 = "str";
        String str2 = "ing";
        String str3 = "str" + "ing";
        String str4 = str1 + str2; //对于字符串变量的“+”连接表达式,它所产生的新对象都不会被加入字符串池中,其属于在运行时创建的字符串,具有独立的内存地址,所以不引用自同一String对象。
        System.out.println("string" == "str" + "ing");// true 只有使用引号包含文本的方式创建的String对象之间使用“+”连接产生的新对象才会被加入常量池中
        System.out.println(str3 == str4);//false
        String str5 = "string";
        System.out.println(str3 == str5);//true

String intern method

        String s1 = new String("tom");
        String s2 = s1.intern();
        //String的intern()方法会查找在常量池中是否存在一份equal相等的字符串,如果有则返回该字符串的引用,如果没有则添加自己的字符串进入常量池。

Frequently Asked Interviews: The following code creates several objects

  String s1 = new String("tom");
  • Class loading is only performed once for a class. "Tom" is already created and resides when the class is loaded (if the "tom" string has been resided before the class is loaded, there is no need to repeatedly create an instance of "tom" for residency). The resident string is placed in the global shared string constant pool.
  • When this code is subsequently executed, the String instance corresponding to the "tom" literal has been fixed and will not be created repeatedly. So this code changes the constant poolCopy the object and put it in the heap, And give the reference to this object in the heap to s1.

Reference article: java virtual machine: runtime constant pool

Two, direct memory

1 Introduction

1. Direct memory is not a part of the data area of ​​the virtual machine at runtime, nor is it a memory area defined by the Java virtual machine specification. But this part of memory may also cause OOM when it is frequently used.
2. Newly added the NIO (New Input/Output) class in JDK1.4, introducing aChannel-based(Channel)With buffer(Buffer) I/O mode, it canUse native library to directly allocate off-heap memory, And then use a DirectByteBuffer object stored in the Java heap as a reference to this memory for operation. This can significantly improve performance in some scenarios because it avoids copying data back and forth between the Java heap and the Native heap.

2. Attention

1. The direct memory allocation of the machine will not be limited by the Java heap size, but the memory is limited by the total memory size (RAM, SWAP, paging file) and the addressing space of the processor.
2. When configuring virtual machine parameters, do not ignore direct memory to prevent OutOfMemoryError.

Guess you like

Origin blog.csdn.net/qq_38350635/article/details/102005806