JVM notes (1) java memory area and memory overflow and object creation, layout and positioning

JVM architecture diagram

Insert picture description here

1. Method area

1. Is a memory area shared between threads

2. Stores information about the class loaded by the virtual machine (including class type, modifiers, method information, and field information), static variables of the class, constants of the final type, fields in the class, method data information, constructor, And the content of the code compiled by the compiler, etc.

But the above is just a specification, and the implementation in different virtual machines is different.

For example: List list=new ArrayList();List is the norm, ArrayList corresponds to different implementations of different virtual machines.

Two typical is the " perpetual generations " (JDK8 before) and " Yuan Space " (JDK8 and beyond)

Instance variables are stored in the heap, regardless of the method area.

Runtime constant pool

The runtime constant pool is part of the method area. Used to store various literals and symbol references generated during compilation .

As the name implies, the constants generated at runtime will naturally be stored here (dynamic).

Such as String's intern () method, if the string constant pool already contains a string equal to this String object, a reference to the object will be returned, otherwise the string contained in the String object will be added to the constant pool.

test:

 public static void main(String[] args) {
  
        String sb=new StringBuilder("哈哈").append("嘿嘿").toString();
        System.out.println(sb.intern()==sb);//true

        String sb2=new StringBuilder("ja").append("va").toString();
        //java在Version类中已经出现,字符串常量池已经有它的引用,因此返回false
        System.out.println(sb2.intern()==sb2);//false
    }

as follows:

package sun.misc;
public class Version {
    private static final String launcher_name = "java";
    private static final String java_version = "1.8.0_231";
    ..............................
}

This class is loaded in the Bootstrap ClassLoader, located in rt.jar \ sun \ misc:
Insert picture description here

abnormal situation

When the method area cannot meet the new memory allocation requirements, an OOM exception is thrown.

When the runtime constant pool fails to apply for memory, an OOM exception will be thrown.

Second, the stack

The stack is responsible for running (heap is responsible for storage)

1. The thread is private and has the same life cycle as the thread , and there is no garbage collection problem

2. Each method execution creates a stack frame and saves it on the top of the stack. Each method corresponds to a stack frame from the stack to the stack from call to execution.

as follows:

	public static void m1(){
        System.out.println("m1..begin");
        m2();
        System.out.println("m1..end");
    }
    public static void m2(){
        System.out.println("m2");
    }
    public static void main(String[] args) {
        System.out.println("main...begin");
        m1();
        System.out.println("main...end");

    }

The corresponding stack model is as follows:

| |

| m2() |

| m1 () |

| main() |

3. Stack frames are used to store information such as local variable tables, operand stacks, dynamic connections, and method exits.

The information stored in the stack (or part of the local variable table) is as follows:

8 basic types of variables + object reference variables + instance methods

Local variable table : store local variables in the method (non-static variables declared in the method, input and output parameters, etc.). 8 basic types of variables directly store the corresponding values, and object reference variables store a reference pointer to the object's starting address, or to a handle representing the object or other object-related location)

Reference to runtime constant pool : When we use constants in a class in a method, we need a reference to the runtime constant pool.

Operand stack : record all calculation-related operations of stacking and stacking in the method

Method exit : After the method ends, we need to know where it was called from before, so we need to save the address returned by a method.

Stack-related exceptions

1. The size of the stack requested by the thread is larger than the size allowed by the virtual machine, and a StackOverflowError error will occur.

2. An OOM exception occurs when the stack application dynamic expansion fails to obtain enough memory. (The current HotSpot virtual machine cannot be dynamically expanded, so after a thread successfully applies for stack space, there will be no OOM exception, but OOM will still occur if the application fails).

Stack exception test:

For the first case, you can reduce the size of the stack:

StackOverflowError appears

public class VMStackSOF {
    private static int stackLength = 0;

    //-Xss150k 减少栈内存容量 StackOverFlow
    public void stackLeak() {
        stackLength++;
        stackLeak();
    }

    public static void main(String[] args) {
        VMStackSOF oom=new VMStackSOF();
       try {
            oom.stackLeak();
        }catch (Throwable e){
            System.out.println("栈长度:"+oom.stackLength);
            throw e;
        }

    }
}

Or change the local variable table of the method frame to be oversized:

public class VMStackSOF {
    private static int stackLength = 0;

    //增大方法帧中本地变量表的长度 StackOverFlowError
    public static void test() {
        long a1,.......a100;//100个局部变量
        stackLength++;
        test();
        a1 =a2=.....a100;
    }

    public static void main(String[] args) {
        VMStackSOF oom=new VMStackSOF();
 
       try {
           test();
       }catch (Error a){
           System.out.println("长度"+stackLength);
            throw a;
       }


    }
}

Three, heap

1. For sharing between threads, created when the virtual machine starts

2. Used to store object instances and arrays

3. The memory area managed by the garbage collector, also known as the "GC heap".

However, it should be noted that with the development of compilation technology, especially the escape analysis technology is becoming more powerful, on-stack allocation and TLAB can be performed.

It can be expanded here:

About allocation on the stack:

There are many objects that do n’t escape the method, and if created on the heap, when the method is called, the life cycle of the object ends, and the reference to the object in the heap accordingly Without it, the GC will reclaim the object. If there are many such objects, the pressure on the GC will increase.

At this time, we can disperse such object attributes and allocate them on the stack, so that after the method call is completed, the recycling of the stack space will recycle the scattered objects. This avoids adding pressure to the GC and improves performance.

About TLAB:

Because the heap is a globally shared memory area, and the creation of objects is a very frequent behavior, so multiple threads need to synchronize when allocating memory to objects concurrently (jvm uses the CAS failure retry method to ensure atomicity) And this overhead is very large in some cases.

The TLAB (Thread Local Allocation Buffer) is to divide the private allocation buffer of multiple threads in the heap, so that when the thread allocates the heap space, it first allocates to its own buffer to avoid the overhead caused by synchronization to improve the object Distribution efficiency. (Jvm is turned on by default, you can also use -XX: +UseTLABdisplay to turn on), but when the object is too large, it still allocates space directly on the heap.

Use the -XX:+PrintTLABparameter to turn on tracking the usage of TLAB,

Use -XX:TLABSizethis parameter to specify the size of the TLAB space allocated to each thread

Composition of heap space

The heap is composed of the old generation and the young generation, and the young generation is divided into the eden area and the two survivor areas (from Space and To Space).

The object is first allocated to the young generation's eden area, but if the object is larger than the eden area and smaller than the old area, it is directly thrown in the old area.

In the survivor area, the objects still alive after many GCs were transferred to the elderly area.

Heap size setting

The -Xms minimum value and -Xmx maximum value control the size of the heap.

When -Xms and -Xmx are the same, the heap is fixed-size and cannot be automatically expanded, otherwise it can be automatically expanded

abnormal

When the memory in the heap is insufficient to allocate object space and the heap cannot be expanded, an OOM exception is thrown.

Anomaly test

Use the above two parameters to control the heap size to 10MB, and add the- XX:+HeapDumpOnOutOfMemoryErrorparameter to dump the current memory heap dump snapshot when OOM appears.

public class HeapOOM {
    static class OOM{

    }

    /**
     * -Xms10m -Xmx10m -XX:+HeapDumpOnOutOfMemoryError
     * -XX:+HeapDumpOnOutOfMemoryError 让虚拟机在出现内存溢出异常时Dump出当前的内存堆转储快照
     * @param args
     */
    public static void main(String[] args) {
        /*
            java.lang.OutOfMemoryError: Java heap space
            Dumping heap to java_pid9368.hprof ...
            Heap dump file created [28275886 bytes in 0.140 secs]
         */
        List<OOM> list=new ArrayList<>();
        while (true){
            list.add(new OOM());
        }
    }
}

We use the IDEA integrated plug-in jprofiler to run, open the heap dump snapshot obtained, you can quickly find the abnormal location.

Insert picture description here

Fourth, the program counter

It can be regarded as the line number indicator of the bytecode executed by the current thread lock . The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed.

Since multiple threads in the java virtual machine are implemented by thread switching and allocating processor execution time, at any one time, a processor will only execute instructions in one thread.

Therefore, in order to restore the execution position before switching after switching threads , each thread needs to have an independent program counter, which does not interfere with each other and is stored independently.

Therefore, the program counter is private to the thread.

If a java method is executed, the instruction address of the byte code of the virtual machine being executed is recorded.

If the local native method is executed, the counter value is undefined.

No OOM exception

Five, local method stack

It is basically the same as the java stack, but the local method stack is to support local native methods, and the stack is for java methods.

The exception occurs in the same way as the stack.

Six, the creation of objects

Only ordinary Java objects are discussed here, excluding arrays and Class objects.

When the virtual machine encounters a new instruction:

1.new instruction

Check whether the parameter of this command can be in the constant pool to target a class of symbolic references whether, and to check the class represented by the reference symbol has been loaded, parsed and initialized. If not, then the corresponding class loading process must be performed first .

2. Allocate memory

The required memory size of the object can be completely determined after the class initialization is completed.

There are two ways to allocate memory:

a. Pointer collision (Bump The Pointer)

When the heap memory is regular, one side is used memory and the other is free memory. A pointer is used as an indicator in the middle. The allocation is to move the pointer to the free side by the same distance as the object.

b. Free List (Free List)

When the heap memory is not regular, the virtual machine maintains a list that records which memory blocks are available. Allocating memory is to find a large enough space from the list to allocate to the object instance and update the list content.

The allocation method is determined by whether the heap is regular, and whether the heap is regular is determined by whether the garbage collector can perform space compression and sorting.

3. Initialization

Initialize the allocated memory space (excluding the object header) to a value of zero. (The TLAB method can be carried out in advance to TLAB allocation) to ensure that the object instance field can be used directly without assigning an initial value.

4. Initial setting of the object

Such as: which instance of the object , how to find the metadata information of the class , the hash code of the object , the age of GC generation, etc.

The above information stored in the object header (Object Header) in. Depending on the current running state of the virtual machine, such as whether to enable bias lock, etc., the object header will have different settings.

5. The <init> () method

The new instruction will then execute the init method of the Class file, that is, the constructor, to construct other resources and status information of the object. Only then is it possible to construct a truly usable object.

Seven, the memory layout of the object

In HotSpot, the storage layout of objects in heap memory can be divided into three parts:

Object header (Header), instance data (Instance Data) and alignment padding (Padding)

7.1 Object header

There are two main types of information:

  1. Used to store the runtime data of the object itself

    Hash code, GC generation age, lock status flag, lock held by the thread, biased thread ID, biased timestamp.

  2. Type pointer

    The object points to a pointer to its type metadata, which determines which class instance is the object. (But the metadata information of the search object does not have to go through the object itself).

    If the object is a java array, the object header also needs to record a length of the array.

7.2 Example data

Stores valid information for objects. That is, the content of various types of fields defined in the code (both inherited by the parent class and subclasses will be recorded).

The storage order is affected by the virtual machine allocation strategy parameter -XX: FieldsAllocationStyle and the field definition order.

The default allocation order is:

Longs / doubles, ints, shorts / charts, bytes / booleans, oops (Ordinary Object Pointers), you can see that fields of the same width are always allocated and stored together.

7.3 Align padding

The automatic memory management system of the virtual machine requires that the object start address must be an integer multiple of 8 bytes, that is, the size of the object must be an integer multiple of 8 bytes. If the instance data is not aligned, it needs to be filled as a placeholder by alignment pad all.

Eight, object access positioning

Java refers to specific objects in the heap through the reference on the stack.

There are two main ways: handle and direct pointer

8.1 Handle access

A memory is divided into the handle pool in the heap, and the reference stores the handle address.

Handle object contains instance data and type data of each of the address information.

8.2 Direct pointer

The reference directly stores the object address . If you only access the object itself, you avoid the overhead of a profile visit.

Advantage

Handle : The reference stores the stable handle address. When the object is moved, the instance data pointer in the handle of the disease is wise.

Direct pointer : It is faster and saves the time overhead of pointer positioning. In frequent Java object access, this overhead is very considerable.

HotSpot mainly uses direct pointers for object access.

======================================================================

Other related notes:

JVM notes (2) The life and death of objects and the four major references to java

JVM notes (3) garbage collection algorithm and HotSpot algorithm implementation (security point, memory set and card table, write barrier, three-color mark, etc.)

JVM notes "Four" seven common garbage collectors

JVM notes (five) class loading mechanism, class loader and parent delegation mechanism

================================================================

Reference:
"In-depth understanding of the third edition of java virtual machine"

Published 75 original articles · won praise 13 · views 8369

Guess you like

Origin blog.csdn.net/weixin_43696529/article/details/104884273