Garbage Collector, java Garbage Collector
Purpose:
The only reason for using the garbage collector is that the recycle program no longer uses the memory.
Target object:
The Java Garbage Collector automatically recycles Java objects that are no longer in use and releases the memory. However, the memory allocated to the heap is recycled from the memory created with new.
Finalize ():
So how can we recycle objects created in this way? For example, if Java calls the local C language method to create an object, the object is not placed on the stack. Unless you call c's free () method manually, this object will never be cleared.
The finalize () method of Java can solve the above problems. When the Garbage Collector recycles a garbage object, it first calls the finalize () method of the object. Therefore, you can call the free () method of c in the finalize () method.
In general, the textbook will write finalize () for the cleanup before garbage collection. In fact, except for the few cases mentioned above, we generally do not need to use finalize ().
Not Guaranteed:
Although the Java garbage collector will automatically clean up the memory based on the usage of the object, it does not necessarily happen. If the memory is sufficient, virtual machines generally do not waste time cleaning.
How to determine whether Java objects can be recycled:
1. The reference counter method is not used ":
Each object contains a reference counter. When a reference variable points to this object, the reference counter + 1 is referenced. When this reference variable no longer points to this object or is set to null, counter-1. For example:
When the fourth case occurs, that is, if no reference variable points to the object "Li Si", the garbage collector recycles the object where Li Si is located at the right time.
It is simple and convenient, but the reason why it is not used by the Java Virtual Machine is that it cannot solve the problem of circular reference. A simple example:
ObjA has an instance variable, and objB also has an instance variable, so that the objA instance points to the B object and the objB instance variable points to the object, the reference counters of object B and object A are both 1 and not 0. According to the Reference Counter method, A and B cannot be recycled, but the fact is, the reference variables objA and objB are already null (the specific objects they point to are no longer referenced ).
2. Root Search Algorithm
In mainstream commercial programming languages (Java, C #, and even the ancient human Lisp Language), the root search algorithm (GC Roots Tracing) is used to determine whether an object is alive.
As mentioned before, object references are placed in the stack, and constant references are placed in the constant pool.
The idea of the root search algorithm is to traverse all the reference variables from the constant pool and the reference variables in the stack and find all the active objects (the reference is not null ). Then, search for all the references contained in the object until all the reference networks are accessed.
The reference variable in the constant pool or stack is the root node, and the expanded network is a reference chain. Finally, if we finally find that the path from an object to the root node is not reachable, it means that this object is recoverable, which solves the problem of circular reference:
For example, GCRoots is the root node. Although object5, 6, and 7 are referenced separately, they are not accessible to GCRoots, so they can be recycled.
How to recycle it?
The recovery algorithm used by each virtual machine is different. The typical case is as follows:
Mark-clearing algorithm:
When the root search algorithm is used to search for referenced variables, the virtual opportunity is used to mark each surviving object and clear all the variables.
The problem is that the surviving objects are not stored continuously in the heap. After the "dead" object is cleared, a large number of fragments will be left in the memory, if you need to use a large memory object later, the memory space is insufficient and you need to refresh the memory. Before recycling:
After recycling:
Copy algorithm:
It divides the available memory into two equal-size blocks by capacity and uses only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space. Before recycling:
After recycling (move the stored objects to the right, all the remaining objects on the Left can be cleared, and then all objects can be cleared. When the right side needs to be cleaned up, similarly, move the surviving object to the left side, and then clear the right side ):
Disadvantages of this method: Obviously, the available memory is only half of the original memory. There is another drawback: if a large number of surviving objects are on the left side, it is a waste of time to move all objects to the right side during cleaning.
Currently, commercial virtual machines use this collection algorithm, but the ratio of the reserved area to the operating area is different, and the heap memory is divided into the new generation and old generation in detail. The Young generation is divided into three regions: Eden, From nation vor, And To nation vor. For more information about the new generation, the old generation, and heap memory, see JavaVM.