Go deep into the Java core and explore the Java garbage collection mechanism (transferred from http://edu.21cn.com/java/g_189_859836-1.htm )
Garbage collection GC Garbage collection is one of the core technologies of the Java language. Previously, we specifically discussed the new features of the garbage collector G1 added in Java 7, but from the perspective of the internal operation mechanism of the JVM, the garbage collection principle and mechanism of java have not changed. The purpose of garbage collection is to remove objects that are no longer used. GC determines whether to collect objects by determining whether objects are referenced by active objects Set the object. GC first determines whether the object is ready for collection. Two common methods are object reference traversal.
Reference count collector
Reference counting is an early policy in the garbage collector. In this approach, Each object (not a reference) in the heap has a reference count. When an object is created and assigned to a variable, the variable count is set to 1. When any other variable is assigned as a reference to the object, the count is incremented by 1 (a = B, then the object referenced by B + 1), but when a reference of an object exceeds the life cycle or is set to a new value, the reference count of the object is reduced by 1. Any object with a reference count of 0 can be garbage collected. When an object is garbage collected, the count of any object it references is reduced by 1.
Advantages: the reference counting collector can be executed quickly and interleaved in the program running. It is beneficial to the real-time environment in which the program is not interrupted for a long time.
Disadvantages: circular references cannot be detected. If the parent object has a reference to the child object, the child object references the parent object in turn. In this way, their reference count can never be 0
Trace collector
Early JVMs used reference counting, but now most JVMs use object reference traversal. Object reference traversal starts with a group of objects and follows each link on the whole object graph, Recursively determine reachable objects. If an object cannot reach from one (at least one) of these root objects, it is garbage collected. In the object traversal phase, GC must remember which objects can reach in order to delete unreachable objects, which is called marking objects.
Next, GC deletes unreachable objects. When deleting, some GCS simply scan the stack, delete unmarked and unmarked objects, and free their memory to generate new objects, This is called sweeping. The problem with this method is that the memory will be divided into many small segments, which are not enough for new objects, but the combination is large. Therefore, many GCs can reorganize the objects in memory and compact them to form available space.
To do this, GC needs to stop other activities. This approach means that all application related work stops and only GC runs. As a result, many mixed requests were added or removed during the response. In addition, more complex GCs are added or run simultaneously to reduce or clear application interruptions. Some GCS use a single thread to do this, while others use multithreading to increase efficiency.
Some common garbage collectors
◆ mark - clear the collector
This collector first traverses the object graph and marks reachable objects, and then scans the stack to find unmarked objects and free their memory. Such collectors typically work with a single thread and stop other operations. Moreover, because it only clears the unmarked objects without compressing the marked objects, it will produce a large number of memory fragments and waste memory.
◆ mark - compression collector
Sometimes called Mark clear compact collector, it has the same marking stage as mark clear collector. In the second stage, the marker object is copied to a new field of the stack to compress the stack. This collector also stops other operations.
Copy collector
This collector divides the stack into two domains, often referred to as half space. Only half of the space is used at a time, and the new objects generated by the JVM are placed in the other half. When GC runs, it copies reachable objects to the other half of the space, compressing the stack. This method is suitable for short-lived objects, and continuous replication of long-lived objects leads to reduced efficiency. And for the specified size heap, it needs twice the size of memory, because only half of it is used at any time.
Incremental collector
The incremental collector divides the stack into multiple domains and collects garbage from only one domain at a time. It can also be understood as dividing the stack into small blocks and garbage collecting only one block at a time. This results in less application interruption time, so that users are generally unaware that the garbage collector is working.
Generation collector
The disadvantage of the replication collector is that all marked objects are copied every time they are collected, resulting in some objects with a long life cycle being copied back and forth many times, which consumes a lot of time. The generational collector can solve this problem. The generational collector divides the stack into two or more domains to store objects with different lifetimes. New objects generated by the JVM are usually placed in one of the domains. Over time, objects that continue to exist (not short-lived objects) will gain their lifetime and move into longer-lived domains. Generational collectors use different algorithms for different domains to optimize performance.
Parallel collector
Parallel collectors use some traditional algorithm and use multithreading to perform their work in parallel. Using multithreading technology on multi CPU machines can significantly improve the scalability of Java applications.
Finally, a very simple example diagram of tracking collector is posted to deepen everyone's understanding of collector: