Java memory area and out of memory exception

runtime data area

The Java virtual machine runtime data area includes: program counter, virtual machine stack, native method stack, heap, method area, and runtime constant pool (part of the method area).

image

program counter

==Thread private==, pointing to the next instruction to execute. The program counter is a small memory space that can be thought of as a line number executor of the bytecode executed by the current thread. This memory area is the only one that does not specify any OOM situation in the JVM specification.

Java virtual machine stack

==Thread private==, ==Life cycle is the same as thread==. Describes the memory model of Java method execution: when each method is executed, a == stack frame == (Stack Frame) is created at the same time for == to store local variable tables, operation stacks, dynamic links, method exits == . The process of each method from invocation to completion of execution corresponds to the process of a stack frame being pushed to the stack in the virtual machine stack. There are two exceptions:
1. If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown.
2. If the virtual machine stack can be dynamically expanded, if enough memory cannot be applied for during expansion, an OOM exception will be thrown.

native method stack

The native method stack serves the native methods used by the virtual machine. Like the virtual machine stack, SO and OOM exceptions are also thrown.

Java heap

==Thread sharing==, due to the generational collection algorithm basically adopted by the collector now, the Java heap can also be subdivided: the new generation and the old generation; more detailed are Eden space, From Survivor space, To Survivor space, etc. . ==All object instances and arrays are allocated on the heap==, which is the main area managed by the == garbage collector==. The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous. If there is no memory in the heap to complete the instance allocation, and the heap can no longer be expanded, an OOM exception will be thrown.

method area

The method area, alias called Non-Heap, is the memory area of ​​== thread sharing ==. The purpose is to distinguish it from the Java heap, == store class information, constants, static variables, code compiled by the just-in-time compiler==. When the method area cannot meet the memory allocation requirements, an OOM exception will be thrown.

runtime constant pool

is part of the method area. It is used to store various literals and symbolic references generated during compilation. This part of the content will be stored in the runtime constant pool of the method area after the class is loaded. When the constant pool can no longer apply for memory, an OOM exception will be thrown.

Exploring HotSpot Virtual Machine Objects

object creation

  1. When the virtual machine encounters a new instruction, it will first check whether the parameters of this instruction can locate a symbolic reference of a class in the constant pool, and check whether the class represented by this symbolic reference has been loaded, resolved and initialized. If not, the corresponding class loading process must be performed first.
  2. Allocate memory for nascent objects. ==Pointer collision==: Assuming that the memory in the Java heap is absolutely regular, all the used memory is put on one side, the free one is put on the other side, and there is a pointer in the middle as a demarcation point. Allocating memory at this time simply moves the pointer to the free space by a distance equal to the size of the object. ==Free list==: If the Java heap memory is not regular, the virtual machine must maintain a list to record which memory blocks are available, find a large enough space from the table to divide it into the object when allocating, and update it list. The choice of allocation method is determined by whether the Java heap is regular or not, and whether it is regular or not is determined by whether the garbage collector has a compaction function. == Therefore, when using collectors with Compact processes such as Serial, ParNew, etc., the pointer collision method is used. When using a Mark-Sweep-based collector such as CMS, the free list method== is used.

In order to ensure that the process of allocating memory space when creating an object is thread-safe, there are two schemes.
1. Synchronize the action of allocating memory space.
2. The action of memory allocation is divided into different spaces according to the thread, == that is, each thread pre-allocates a small piece of memory in the Java heap, which is called the local thread allocation buffer==.

After the memory allocation is completed, the virtual machine needs to initialize the allocated memory space == with a zero value ==. Next, make the necessary settings for the object.

Object's memory layout

In the HotSpot virtual machine, the layout of objects in memory can be divided into 3 blocks: object header, instance data and alignment padding==.
1. Object header: Contains runtime data and type pointers that store the object itself. If the object is an array, there is also a piece of data that records the length of the array.
2. Instance data: The valid information that the object actually stores.
3. Align padding: no special meaning, placeholder. Because the size of the object must be an integer multiple of 8 bytes.

object access location

The object is created for exclusive use, and the Java program needs to manipulate the specific object of the object through the reference data on the stack.
1. Use the handle method: The Java heap will divide a piece of memory as a handle pool. Stored in reference is the address of the object handle. The object handle stores the address of the instance data and the address of the type data. The advantage is that when the object is moved, only the instance data pointer in the handle is changed, and the reference itself does not need to be modified.
2. Use a direct pointer: The address stored in referen is the address of the object. Advantages: faster, saves the time overhead of a pointer positioning. HotSpot uses direct pointers for object access.

OutOfMemoryError exception

Java heap overflow

Limit the size of the Java heap to 20MB, which is not expandable (-Xms: the minimum value of the heap, -Xmx: the maximum value, set the same to avoid automatic expansion)

// VM Args: -Xms20m -Xmx20m
public class HeapOOM{

    static class OOMObject{

    }

    public static void main(String[] args){
        List<OOMObject> list = new ArrayList<>();

        while(true){
            list.add(new OOMObject());
        }
    }
}

A common memory overflow situation in practical applications when the OOM exception of Java heap memory occurs.

Virtual machine stack and native method stack overflow

The stack size is only set by the -Xss parameter.
1. If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, a StackOverflowError exception will be thrown.
2. If the virtual machine cannot apply for enough memory when expanding the stack, an OutOfMemoryError exception will be thrown.

Method area and runtime constant pool overflow

Native direct memory overflow

Guess you like

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