reference counting algorithm

 

Before the garbage collector collects the heap, the first thing to do is to determine which of these objects are still "alive" and which are "dead" (that is, objects that can no longer be used by any means).

1) Reference counting algorithm

Add a reference counter to each object. Whenever there is a place to refer to it, the counter value is incremented by 1; when the reference is invalid, the counter value is decremented by 1: an object whose counter is 0 at any time cannot be used anymore. . The reference counting algorithm (Reference Counting) is simple to implement and has high judgment efficiency. In most cases, it is a good algorithm. The reference counting algorithm is not used in the Java virtual machine to manage memory. The main reason is that it is difficult to solve the problem of mutual circular references between objects.

2) Reachability Analysis Algorithm

It is through a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, and the path traversed by the search is called the Reference Chain. When an object has no reference chain to the GC Roots When connected (in Tucker's words, the object is unreachable from the GC Roots), it proves that the object is unavailable.

 

Objects that can be used as GC Roots include the following categories:

  • Objects referenced in virtual machine money (local variable table in stack frame).

  • The object referenced by the static properties of the class in the method area.

  • The object referenced by the constant in the method area. .

  • Identifier of JNI (native method) references in native method warfare.

 

 

3) Talk about citations again

After JDK 1.2, Java expanded the concept of references and divided references into strong references, soft references, weak references, and Phantom References. Their reference strengths gradually weakened.

  • Strong references are those that are ubiquitous in program code. Similar to Object obj = new Object(), as long as the strong reference still exists, the garbage collector will never reclaim the referenced object.

  • Soft references are used to describe objects that are still useful but not required. For the object to which the soft reference is attached. Before the system is about to generate an out-of-memory exception. These objects will be listed in the garbage collection scope for a second collection. If there is still not enough memory for this collection, a memory exception will be thrown. After JDK1.2, the Soft Reference class is provided to implement soft references.

  • Weak references are also used to describe non-essential objects. Objects associated with weak references can only survive until the next garbage collection occurs. When the garbage collector works, no matter whether the current memory is enough, it will collect objects that are only associated with weak references. After JDK 1.2, the WeakReference class is provided to implement weak references.

  • Phantom references, also known as ghost references or phantom references, are the weakest kind of reference relationship. Whether or not an object has a virtual reference will not affect its lifetime at all, nor will an object instance be obtained through a virtual reference. The only purpose of setting a phantom reference association for an object is to receive a system notification when the object is reclaimed by the collector. After JDK 1.2, the PhantomReference class is provided to implement phantom references.

4) To live or to die

Even an object that cannot be reached in the reachability analysis algorithm is not "necessary to die". To truly declare an object dead, at least two marking processes are required: if the object finds out after the reachability analysis If there is no reference chain connected to GC Roots, it will be marked for the first time and screened once. The screening condition is whether it is necessary to execute the finalize() method on this object. When the object does not override the fnalize() method, or the finalize() method has been called by the virtual machine, the virtual machine treats both cases as "not necessary, to be executed".

If the object is determined to be necessary to execute the finalize() method, then the object will be placed in a queue named F-Queue, and later by a low-priority queue automatically created by the virtual machine. Finalizer thread to execute. "Execute" means that the virtual machine triggers the method, but does not promise to wait for it to finish running. The finalize (www.2636666.cn) side is the last chance for the object to escape the fate of death. Later, the GC will mark the object in the F-Queue for the second time on a small scale. If the object has not escaped at this time, it can be Recycled. 

5) Method recovery area

The method area (HotSpot virtual machine permanent generation) garbage collection mainly recycles two parts: discarded constants and useless classes. A class needs to meet the following three conditions at the same time to be considered a "useless class":

  • All instances of this class have been reclaimed, that is, there are no instances of this class in the Java heap.

  • The ClassLoader that loaded the class has been recycled.

  • The java.lang www.wanmeiyuele.cn Class www.mhylpt.com/ object corresponding to this class is not referenced anywhere, and the methods of this class cannot be accessed through reflection anywhere

The virtual machine can recycle the useless classes that meet the above three conditions, but not necessarily.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214890&siteId=291194637