Java virtual machine common configuration and garbage collection principle

1. Java virtual machine runtime data area:

 

Heap -Xms heap initial memory-Xmx heap maximum memory The memory area shared by each thread, the main area of ​​garbage collection is used to store object instances, not all object instances are stored in the heap, escape analysis technology, stack allocation, scalar replacement optimization technology Some object instances can be stored directly on the stack. The heap can be divided into the new generation and the old generation.

method area

-XX:MaxPermSize permanent generation maximum memory

-XX:PermSize permanent generation initial memory

 

memory area shared by threads

It is used to store data such as class information, constants, static variables, code compiled by the just-in-time compiler that has been loaded by the virtual machine, the logical part of the Java virtual machine heap.

HotSpot uses the permanent band to implement the method area, and the constant pool in JDK1.6 and earlier is placed in the method area

When the method area cannot meet the memory allocation requirements, an OutOfMemoryError will be thrown

Virtual machine stack -Xss stack memory thread private If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError will be thrown. If the virtual machine stack can be dynamically expanded, and the expansion cannot apply for enough memory, the OOM
native method will be thrown stack thread private - Xoss native method stack size (this parameter is invalid when using HotSpot VM)

 

HotSpot virtual machine stack and native method stack combined into one

Like the virtual machine stack, the native method stack also throws StackOverflowError and OOM

 

program counter

thread private

 No configuration, the program counter is a small memory space that can be seen as a line number indicator of the bytecode executed by the current thread

The only area where no OOM situation is specified in the Java Virtual Machine Specification

 

2. Garbage collection

 

reference counting

Add a reference counter to the object. Whenever there is a place to refer to it, the counter value is incremented by 1, and the counter value is decremented by 1 when the reference expires; the object whose counter value is 0 at any time is impossible to be used again. A few virtual machines use this method

Advantages: simple implementation, high judgment efficiency

Disadvantage: Difficult to solve the problem of circular references between objects

 

accessibility analysis

Through a series of GC root nodes like down search, the path like down search becomes a reference chain. When an object does not have any reference chain connected to the GC root node, it proves that the object is unavailable. Mainstream virtual machines mostly use this method

Advantage: solves the circular reference problem

Disadvantages: Inefficient, and the entire system is frozen when enumerating the root node, which will cause GC pauses (Sun calls it "Stop The World")

 

The unreachable objects in the reachability analysis algorithm are not necessarily recycled. The recycling process: the unreachable objects will be screened once (objects that do not meet the screening conditions will be recycled): the object overrides finalize() and this method does not After being called by the virtual machine, the filtered objects will be put into a queue to execute finalize() in turn. If the object successfully establishes a reference chain with the GC root node when finalize() is executed, it will not be recycled.

 

 

 

 

Guess you like

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