JVM memory structure:

Divided into five structures:

1. Program counter:

The line number indicator of the bytecode executed by the current thread. The thread counters are independent of each other and are called “thread private” areas of memory.

2.Java vm stack:

The memory model used to describe Java methods. Each method execution creates a stack frame to store information, including local variable tables, operand stacks, dynamic links, and method exits. The process from invocation to execution of each method corresponds to the process from loading to loading of a stack frame in the virtual machine. (StackOverflowError/OutofMemoryError)

3. Local method stack:

Use to service Native methods. Basically the same as ↑.

4. The Java heap:

An area shared by all existing objects whose sole purpose is to hold object instances. GC “heap” : New generation./ old age

5. Method Area:

Each thread shares an area, as in ↑. Storing similar data in the heap is a logical part of the heap. (OutofMemoryError)

The GC recycling

Thoughts on garbage collection: what/when/how

What:

Memory space in memory that is no longer used is garbage. (Four references in Java)

How:

Reference counting method

Counter +1 when the object is referenced; Counter value -1 when reference is invalid. It is difficult to solve the problem of circular reference between objects (object AB refers to each other, reference count each other is 0, cannot be reclaimed).

Accessibility analysis

GC Roots is used as the starting point to search downward, and the search path is called reference chain. When an object is unreachable without any reference links, that is, the object is not available and is therefore considered recyclable.

Objects that can be used as GC Roots: static properties of reference object/method area in virtual machine stack, constant reference object /Native method reference object.

The when:

Reference counting: When an object becomes garbage, the program can immediately sense and recycle it. Reachability analysis: Garbage objects are not sensed until GC is performed.