- Describe the Java garbage collection mechanism.
- What is GC? Why do we use GC?
- Advantages and principles of garbage collection, and describe two recovery mechanisms.
- What is the basic principle of the garbage collector? Can the garbage collector reclaim memory immediately? What's the way to actively notify the virtual machine for garbage collection?
- What are the reference types in Java?
- How to determine whether the object can be recycled?
- When can objects be garbage collected in Java
- Will garbage collection happen in the permanent generation in the JVM
- What are the garbage collection algorithms in the JVM?
- What are the garbage collectors in the JVM?
In Java, the programmer does not need to display to release the memory of an object but is executed by the virtual machine itself. In the JVM, there is a garbage collection thread, which is of low priority and will not be executed under normal circumstances. Only when the virtual machine is idle, or the current heap memory is insufficient will the execution be triggered. Sweep the objects that are not referenced by any, and add them to the collection to be collected for recycling.
GC is the meaning of garbage collection. Memory processing is the place where programmers are prone to problems, forgetting, or making mistakes. Recycling can lead to program or system instability or even crash. The GC function provided by Java can automatically monitor whether an object is out of scope to achieve automation. Java does not offer a display operation method to release the allocated memory.
The most remarkable feature of Java language is the introduction of garbage collection mechanisms, making Java programmers no longer consider the problem of memory management when writing programs.
Because of this garbage collection mechanism, objects in Java no longer have the concept of "scope." Only referenced objects have "scope."
The garbage collection mechanism effectively prevents memory leakage and can effectively use available memory.
The garbage collector usually runs as a separate low-level thread, which can clean up and recycle the dead or unused objects in the memory heap in unpredictable circumstances. The programmer can't call the garbage collector for garbage collection on an object or all objects in real-time. Garbage collection includes generational replication garbage collection, labeled garbage collection, and incremental garbage collection.
For a GC, when a programmer creates an object, the GC starts to monitor the address, size, and usage. In general, GC records and manages all objects in a heap using a directed graph. In this way, we can determine which objects are reachable and which are not. When the GC determines that some objects are "unreachable," it is the GC's responsibility to reclaim these memory spaces.
Of course, programmers can do it manually through System.GC (), which tells the GC to run, but the Java language specification does not guarantee that the GC will execute.
Strong reference: GC is not recycled when it occurs.
Soft references: useful but not necessary objects that are recycled before a memory overflow occurs.
Weak references: useful but not necessary objects that will be recycled the next GC.
Virtual reference (phantom reference/phantom reference): the object cannot be obtained by virtual reference. The phantom reference is used to implement virtual reference. The purpose of a virtual reference is to return notification in GC.
When the garbage collector does garbage collection, the first thing to determine is which memory needs to be recycled, which objects are "alive" and can not be recycled, and which objects are "dead" and need to be recycled.
Generally, there are two ways to help make a decision:
Reference counter method: create a reference count for each object. When there is an object reference, the counter should plus one. When the reference is released, the counter should minus one. Whenever the counter is zero, it can be recycled. It has a disadvantage that it can't solve the problem of circular reference; the Reachability analysis algorithm starts from the GC roots, the path to search is called the reference chain. When an object has no chain of references to GC roots, it can be recycled.
When an object becomes inaccessible to the application currently using it, it can be recycled.
Garbage collection will not occur in the permanent generation. If the permanent generation is full or exceeds the critical value, the full GC will be triggered. If you look closely at the garbage collector's output, you will find that the permanent generation is also recycled. This is why the correct permanent generation size is significant to avoid full GC.
Garbage collection will not occur in the permanent generation. If the permanent generation is full or exceeds the critical value, the full GC will be triggered. If you look closely at the garbage collector's output, you will find that the permanent generation is also recycled. This is why the correct permanent generation size is very important to avoid full GC. Please refer to Java 8: from permanent generation to the metadata.
Mark clear algorithm: Mark useless objects, and then clean them up. Disadvantages: low efficiency, unable to remove debris.
Copy algorithm: divide two equal-sized memory areas according to the capacity, copy the living objects to another block when one block is used up, and then clean up the used memory space once again
Disadvantages: low memory usage, only half of the original.
Mark organize algorithm: Mark useless objects, make all the living objects move to one end, and then directly clear the memory outside the end boundary.
Generational algorithm: according to the different life cycles of the object, the memory is divided into several blocks, generally the new generation and the old generation. The new generation uses the replication algorithm, and the old generation leverages the mark sorting algorithm.
Serial collector (Replication Algorithm): in the new generation of single thread collectors, marking and cleaning are individual threads, with the advantages of simplicity and efficiency;
Pardew collector (Replication Algorithm): the new generation Parallel collector is the multithreaded version of the serial collector, which has better performance than serial in multi-core CPU environment;
Parallel scavenge collector (Replication Algorithm): a new generation of parallel collectors, pursuing high throughput and efficient use of CPU. Throughput = user thread time / (user thread time + GC thread time). High throughput can efficiently use CPU time to complete the program's operational tasks as soon as possible, which is suitable for background applications and other scenarios with low requirements for interaction;
Serial old collector (tag grooming algorithm): single thread collector of the old generation, the old age version of the serial collector;
Parallel old collector (tag grooming algorithm): parallel collector in the old days, throughput first, the old version of the parallel scavenge collector;
CMS (concurrent mark sweep) collector (clear mark algorithm) is a parallel collector in the old days, aiming to obtain the shortest recovery pause time. It has the characteristics of high concurrency and low pause and pursues the shortest GC recovery pause time.
G1 (garbage first) collector (tag defragmentation algorithm): Java heap parallel collector. G1 collector is a new collector provided by JDK 1.7. G1 collector is implemented based on the "tag defragmentation" algorithm; that is, it will not generate memory fragmentation. Besides, the G1 collector is different from the previous collectors in that the scope of G1 recycling is the entire Java heap (including the new generation and the old generation), while the scope of the first six collectors is only limited to the new generation or the old generation.