Judging the survival of the object and the reference of the object

1. Judging the survival of the object

Almost all object instances are stored in the heap. Before the garbage collector recycles the objects, the thing to do is to determine which of these objects are still "surviving" and which are "dead" (death means that it is impossible to be reused) Any way to use the object)

1.1, reference counting algorithm

Add a reference counter to the object. Whenever there is a reference to it, the counter will be incremented by 1;

As shown in the following code:

String p = new String("abc");

insert image description here

When the method of our above code ends, or we manually remove its reference

p = null;

insert image description here

The above is our reference counting algorithm. Its advantages are: fast, convenient, and simple to implement, but it has disadvantages, that is, when objects refer to each other, it is difficult to judge whether the object should be recycled.
insert image description here
In the above schematic diagram, there are mutual references between objects, resulting in ring data. Even if the ring data has no use, but because their references are not 0, they will never be recycled, which will cause "memory leaks" "The problem.


1.2. Accessibility analysis algorithm

Use a series of objects called "GC Roots" as the starting point, and start searching downward from these nodes. The path traveled by the search is called a reference chain (Reference Chain). When an object is connected to GC Roots without any reference chain , it proves that the object is unavailable.

The objects used as GC Roots mainly include the following:

  • Objects referenced in the virtual machine stack (local variable table in the stack frame).
  • The object referenced by the class static property in the method area.
  • The object referenced by the constant in the method area.
  • The object referenced by JNI (that is, the Native method in general) in the local method stack.

insert image description here

1.3, finalize method

Even if the unreachable object is judged by the reachability analysis algorithm, it is not "must die", it will still be in the "probation" stage. To really declare an object dead, it needs to go through two marking processes, one is not found and GCRoots , it will be marked for the first time. Then perform a screening (if the object overrides the finalize method), we can save it in finalize.

However, it is generally not recommended to use this method, because the priority of this method is low, the order of its calls cannot be guaranteed, and its uncertainty is large; in addition, this method will only be automatically called once by the system, and will not be called the second time. It is generally recommended to process in try-finally.


1.4. Recycling in the method area

The Java virtual machine specification does not require the heap method area to implement garbage collection, because the efficiency of garbage collection in the method area is low. The main content of garbage collection in the general method area is: discarding constants and useless classes.

For example, if a string "abc" enters the constant pool, if there is no String object named "abc" in the current system, that is, there is no String object referencing the string in the constant pool, this is the "abc" constant. can be recycled.

As mentioned above, it is relatively simple to judge whether a constant can be recycled, but the conditions for garbage collection of useless classes are more stringent, and the following points need to be met:

  • All instances of this class have been recycled, that is, there are no instances of this class in the heap.
  • The ClassLoader that loaded this class has been recycled.
  • The java.lang.Class object corresponding to this class is not referenced anywhere, and the method of this class cannot be accessed through reflection anywhere.
  • It is necessary to ensure the setting of the relevant parameters of the virtual machine, which -Xnoclassgcis used to control whether to recycle the class.

Second, the object reference

Whether judging the number of references to an object through the reference counting algorithm, or judging whether the reference chain of the object is reachable through the reachability analysis algorithm, judging whether the object survives is related to "references". After JDK1.2, the concept of reference has been expanded in Java, as follows:

2.1, strong reference

It refers to the ubiquitous references like this in the program code Object ojb = new Object(). As long as the strong reference still exists, the garbage collector will never recycle the referenced object.

2.2, soft reference

Used to describe some useful but not required objects. For the objects associated with soft references, before the system discovers the memory overflow exception, these objects will be included in the recycling range for the second recycling. If there is not enough memory for registration and recycling, an out-of-memory exception will be thrown. After JDK1.2, the SoftReference class is provided to implement soft references.

For example, a program is used to process pictures provided by users. If all the pictures are read into the memory, although the pictures can be opened quickly, the memory space will be huge, and some less used pictures waste memory space and need to be manually removed from the memory. If every time a picture is opened, it is read from the disk file to the memory and then displayed. Although the memory usage is small, some frequently used pictures need to access the disk every time they are opened, and the cost is huge. At this time, you can use soft references to build caches.

2.3, Weak citations

Used to describe non-essential objects, but its strength is weaker than soft references, and 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 sufficient or not, objects that are only associated with weak references will be reclaimed. After JDK1.2, the WeakReference class is provided to implement weak references.

Note: Soft reference SoftReference and weak reference WeakReference can be used in the case of tight memory resources and the creation of data caches that are not very important. When the system memory is insufficient, the content in the cache can be released.

Practical application: ThreadLocal, WeakHashMap

2.4. Phantom references

Also known as ghost reference or phantom reference, it is the weakest kind of reference relationship. Whether an object has a virtual reference will not affect its lifetime at all, and an object instance cannot be obtained through a virtual reference. The only purpose of setting a phantom reference to an object is to receive a system notification when the object is reclaimed by the collector. After JDK1.2, the PhantomReference class is provided to implement virtual references.

Guess you like

Origin blog.csdn.net/rockvine/article/details/124608548