JVM memory area
1. Criteria for the subject to be judged to be garbage
It is not referenced by other objects
2. An algorithm to determine whether an object is spam or not
2.1 Reference Counting
Reference counting algorithm
(1) Determine whether an object can be recycled by judging the number of references to the object
(2) Each object instance has a reference counter, +1 for the referenced and -1 for the complete
Merit:
The execution efficiency is high, and the program execution is less affected
Shortcoming:
Circular references could not be detected, resulting in a memory leak
2. Reachability analysis algorithms
GCROOT diagram
Determines whether an object can be recycled by determining whether the object's reference chain is reachable
Reachable reference objects that GCRoot is connected to are marked as alive
It can be seen as an object of GCRoot
1. Objects referenced in the virtual machine stack (local variable table in the stack frame)
2. Objects referenced by constants in the method area
3. Objects referenced by class static properties in the method area
4. The reference object of the JNI (native method) in the local method stack
3. What you know about the garbage collection algorithm
JVM typical garbage disposal
3.1 Mark and Sweep
mark-sweep algorithm
Marking: Scans from the root collection to tag surviving objects
The reachability algorithm finds garbage objects
Clear: Performs a linear traversal of the heap memory from beginning to end to reclaim the memory of unreachable objects
Disadvantages: Memory fragmentation is possible
3.2 Coping
Replication algorithms
It is divided into object polygons and idle polygons
The object is created on the face of the object
The surviving object is copied from the object face to the idle face
Clears all object memory of the object face
Peculiarity:
1. Solve the problem of fragmentation
2. Sequential memory allocation, simple and efficient
3. It is suitable for scenarios with low survival rate of objects
3.3 Compacting
Tag finishing algorithms
Marking: Scans from the root collection to tag surviving objects
Clear: Moves all surviving objects, in order of memory addresses
Then, all the memory after the end memory address is reclaimed
Peculiarity:
1. Avoid non-contiguous rows in memory
2. There is no need to set up two pieces of memory to be interchanged
3. It is suitable for scenarios with high survival rate
3.4 Generational Collector
Generational collection algorithms
Abbreviated as GC garbage collection algorithm
A combo punch of garbage collection algorithms
Divide the object into different regions according to its lifecycle to employ different garbage collection algorithms
Classification of GC
Minor GC
Full GC