Preliminary Discussion on Data Area and Objects of Java Virtual Machine

The memory area and memory overflow exception of the Java virtual machine

1. Runtime data area

Program count area, virtual machine stack, local method stack, Java heap, method area, runtime constant pool

  1. Program count area: a small memory area pointing to the currently executing bytecode line number

    • Role: branch jump, loop... Exception handling, thread recovery, etc. all depend on this area
    • Thread-private: Since each thread executes code at a different location, this area is thread-private
  2. Virtual machine stack: save the memory model of the thread execution method. It is a stack structure. Each stack frame saves information such as the local variable table, operand stack, and exit address of the method; the process of executing the method is like the process of popping the stack and pushing it into the stack

    • Local variable slots: long, double, variable, etc. occupy two variable slots, the specific implementation size depends on the specific virtual machine
    • Thread private: each thread maintains its own method stack
    • Local method stack: similar to the virtual machine stack, but saves the model of the local method (can also be merged)
  3. Java heap: The largest area of ​​memory managed by the virtual machine, storing object instances (Class information is also an object instance of an object Class)

    • GC (Garbage Collection) main area: Based on the generational theory, it can be divided into the new generation area (Eden+Survivor), the old generation area, etc.

    • Thread sharing: Objects can pass information between threads, and threads share objects (note thread safety issues)

    • Method area: a logical part of the heap (also called 'non-heap'), which holds some specific information (type information, constants, static variables... .)

    • Runtime constant pool: part of the method area, which saves literal and symbol references loaded at runtime

  • Direct memory: The part that does not belong to the memory of the Java virtual machine, the space directly opened by the local method, and the memory area that can be directly manipulated by reference
    • Based on the I/O method of channels and buffers, call the local method to open up the local heap memory space, and then directly operate the memory area through DirectByteBuffer, which is not part of the memory management of the Java virtual machine

2. Object Exploration

Object creation process (six steps), object memory layout (three parts), object access positioning (two methods)

  • object creation process
    1. Look up a symbolic reference to a class in the constant pool
    2. Determine whether the class information of the class is loaded into the heap, and execute the class loading if not (Chapter 7: The allocation and initialization of class members are completed here)
    3. Allocate memory (open up memory space for created objects: pointer collision method, free list method), memory allocation synchronization
    4. The object instance is initialized to 0 (all object members become default values)
    5. Object header information setting
    6. Execute the constructor
  • Memory allocation method: use according to the memory space after GC
    • Pointer collision method: In a continuous memory space, the allocation pointer moves to the other end to allocate memory space
    • Free linked list method: In the fragmented space, the fragmented space is allocated in series through the linked list (it does not require the allocation of address space to be continuous)
  • Memory allocation synchronization method: due to possible conflicts in memory allocation when different threads create objects
    • CAS+ failure retry
    • Local thread allocation cache: each thread allocates an address first, each uses its own address first, and then considers allocation conflicts if it is not enough
  • The memory layout of the object: what format does the object exist in memory, each part contains content, etc. (header + instance + padding)
    • Object header: save the object's own runtime data (HashCode, GC age, lock status, thread holding lock status...), type pointer (pointing to class information), array object also saves its own length information
    • Instance data: Whether it is a member of its own object or a member inherited from a child parent class, it is recorded here
    • Padding data: meaningless padding to ensure that the size conforms to the specification
  • object access method
    • Handle access method: Use a 'comparison table' in the heap to record the specific location of the object, and the variable points to the corresponding location in the comparison table (the change of the object location will not cause the variable content to be modified), and an additional table must be maintained
    • Direct pointer method: the variable directly points to the object location without transit

3. Area where memory overflow occurs

Under the automatic recycling mechanism, there is a high probability of memory overflow (insufficient memory space, and existing objects must be retained), rather than memory leaks (some unnecessary content cannot be recycled due to programming reasons). Unreasonable (opening array is too large, too many recursive calls, etc.)

In addition to the program count area, memory overflow may occur in both the virtual machine stack and the Java heap

Guess you like

Origin blog.csdn.net/caqjeryy/article/details/124001896