First, Introduction
- GC (Garbage Collection), garbage collection
- In Java, GC objects are heap space and permanent zone
Second, GC algorithm 1. Reference counting method
- Veteran garbage Collection algorithm
- Recycle Garbage by reference calculation
- Not used in Java, users have COM, ACTIONSCRIPT3, Python
Realize:
1> for an object A, if any one object references a, the reference counter of A is incremented by 1;
2> when the reference fails, the reference counter is reduced by 1;
3> if the value of the reference counter of object A is 0, then object A cannot be used again.
Disadvantages:
- Referencing and de-referencing accompany addition and subtraction, affecting performance
- Difficult to handle circular references
2. Tag-Clear algorithm
- The mark-and-sweep algorithm is the basic idea of modern garbage collection algorithm.
- The tag-purge algorithm divides garbage collection into two phases: the tagging phase and the purge phase.
- Realize:
- In the tagging phase, all objects starting from the root node are marked with the root node first. Therefore, an object that is not marked is a garbage object that is not referenced.
- In the purge phase, all unmarked objects are cleared.
The exact process is as follows:
Disadvantages:
- Generates a large amount of discontinuous memory fragmentation. This can lead to the inability to find enough contiguous memory space when a large object needs to be allocated, and the need to do another garbage collection action in advance.
3. Tag-Compression algorithm
- The tag-compression algorithm is suitable for applications where there are many surviving objects, such as the old age.
- It does some optimizations based on the mark-and-sweep algorithm.
- Like the mark-and-sweep algorithm, the tag-compression algorithm first needs to start with the root node and mark all objects that can be reached.
- However, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory.
- Finally, clean up all the space outside the boundary.
The exact process is as follows:
4. Copying algorithms
- Compared with the tag-purge algorithm, the replication algorithm is a relatively efficient recovery method
- Not suitable for situations where there are many surviving objects, such as the old age
- Divide the original memory space into two pieces, using only one piece at a time
- During garbage collection, the surviving objects in the memory being used are copied to the unused block of memory.
- After that, all objects in the memory block being used are cleared,
- Finally, swap the two memory roles to complete the garbage collection
The exact process is as follows:
Disadvantages:
- Space wasted, memory reduced to half the original.
5. Generation of collection algorithms
- The generational collection algorithm is the algorithm used by most of the JVM's garbage collectors today.
- Classification according to the life cycle of the object: the short-lived object belongs to the Cenozoic, and the long-term object belongs to the old age.
- According to the characteristics of different generations, select the appropriate collection algorithm
- Small number of objects surviving (Cenozoic), suitable for replication algorithms
- Large number of objects surviving (old age), suitable for tag cleanup or tag compression
Third, Stop-the-world
- A phenomenon of global pauses in Java
- Global pause, all Java code stops, native code can execute, but cannot interact with JVM
- Mostly due to GC
- Dump thread
- Deadlock Check
- Heap Dump
Why is there a global pause when GC?
Analogy in the party to clean the room, party is messy, and a new garbage production, the room is never clean, only let everyone stop activities, to the room clean.
Harm:
- Long service stop, no response
- Encountering an HA system can cause primary and standby switching, seriously compromising the production environment.
Java Virtual Machines (iii): GC Algorithms and types