How to judge whether the subject is alive?

How to judge whether the subject is alive?

First of all, why it is necessary to judge whether it is alive or not. When the garbage collector collects the heap, the first thing is to determine which objects are still being referenced or need to be referenced later, that is, live and which are "dead" ( That is, it can no longer be used in any way)

1. Reference counting algorithm

  Add a reference counter to the object. Whenever there is a reference to it, the counter value is increased by 1, and when the reference is invalidated, it is decreased by 1. An object whose counter is 0 at any time cannot be used. This method is very efficient and is also a very good algorithm in most cases.

  However, it is difficult to solve the problem of mutual circular references between objects in the JVM. If two objects call each other, a deadlock-like situation will occur at this time, that is, the mutual call in this place will make the reference counting method always think If an object is referencing the current object, the count value is always greater than or equal to 1, and the GC collector cannot be notified to reclaim them. But the actual situation is that these two objects are no longer called later, so although this method is simple and efficient, it is not our first choice. The virtual machine does not use this algorithm to determine whether the object is alive.

2. Reachability analysis algorithm

  Use a series of GC Roots objects (including: objects referenced in the virtual machine stack, objects referenced by class static properties in the method area, objects referenced by constants in the method area, and objects referenced by JNI in the local method stack) as a starting point. The node starts to search downwards, and objects that are not linked by GCRoots can be recycled. Objects 4 and 5 in the following figure are judged as recyclable objects.

  

  After JDK1.2, Java has expanded the concept of references, that is, objects not only have the concepts of reference and no reference, but have expanded to four:

  Strong reference: similar to "Object obj=new Object()" As long as the strong reference exists, the garbage collector will never reclaim the referenced object.

  Soft reference: It is used to describe some useful but not necessary objects. For soft reference objects, before the memory overflow exception, these objects will be listed in the recovery range for a second recovery.

  Weak references are weaker than soft references. Objects associated with weak references can only survive until the next garbage collection occurs. When garbage collection occurs, regardless of whether the memory is sufficient, only weakly referenced objects are reclaimed.

  Phantom reference, the weakest reference relationship, whether an object has a phantom reference has no effect on its lifetime. The only purpose is to receive a system notification when this object is collected by the collector.

  For the object to truly declare "dead", it needs at least two marking processes. When the object is found to be not chained by GC Roots during the reachability analysis, the object will be marked for the first time and be screened for the first time. It is to determine whether the object needs to execute the finalize() method, and if it needs to be executed, the object will be placed in the queue of the F-Queue to execute the finalize() method in the object. If the finalize() method allows the object to be chained by GC Roots again, the object will survive again, otherwise it will be marked a second time, waiting for the arrival of garbage collection

Can be used as the object of GC Roots:

  1. Objects referenced in the virtual machine stack;

  2. Objects referenced by static properties of the class in the method area;

  3. Objects referenced by constants in the method area;

  4. Objects referenced by JNI in the local method stack;

Recommended reading:

  • In-depth analysis of HashMap and ConcurrentHashMap source code and underlying principles

  • Design pattern (2): detailed explanation of several factory patterns

  • Five mechanisms and advantages and disadvantages of process synchronization (translation)

  • Redis implementation of five data types, commonly used commands, application scenarios

  • The similarities, differences and application scenarios of redis and memcahed

  • Explain the three-way handshake and four-time wave of TCP and the interview questions (very comprehensive)

  • Arrays tool class detailed explanation (super detailed)

  • Algorithms must master several methods

  • QPS, TPS, number of concurrent users, throughput

  • Singleton pattern

  • Collections tools detailed explanation (super detailed)

Guess you like

Origin blog.51cto.com/14977428/2545018