The JVM's garbage collector has different ways of handling different types of references. In Java, for an object, only the existence of a reference, it will always exist in memory. Given the increasing number of objects that exceed the total amount of memory in the JVM, the JVM throws a outofmemory error. Although the detailed execution of garbage collection is controlled by the JVM. However, developers can still interact with the garbage collector to a certain extent, with the goal of helping the garbage collector manage the memory of the application in a better way. This interaction is the JAVA.LANG.REF package introduced with JDK1.2.
Strong references (strong reference)
In a generic Java program, the most visible is a strong reference (strong reference). As Date date = Newdate (), date is a strong reference to an object. A strong reference to an object can be passed around in a program.
In very many cases, there are multiple references to the same object at a time. The presence of a strong reference limits the time that an object survives in memory. If object A contains a strong reference to object B. Under normal circumstances, object B will not survive shorter than object A.
If object A does not explicitly set the reference to object B to null, only if object A is garbage collected, object B no longer has a reference to it. Chance of getting garbage collected.
Soft references (soft reference)
Soft references (soft reference) are weaker than strong references in strength and are represented by class SoftReference.
Its role is to tell the garbage collector which objects in the program are less important and can be temporarily reclaimed when the memory is low. When there is not enough memory in the JVM. The garbage collector releases objects that are only pointed to by soft references.
It is assumed that after all these objects have been freed, the memory is not sufficient to throw a outofmemory error. Soft references are good for creating caches. When the system is running out of memory, the content in the cache can be freed.
For example, consider an image editor program. The program will read all the contents of the image file into memory. To facilitate the processing. Users can also open multiple files at the same time.
When too many files are opened at the same time, it can cause insufficient memory. Assuming that a soft reference is used to point to the contents of the image file, the garbage collector can reclaim the memory when necessary.
Import Java.lang.ref.softreference;public class ImageData {private String path;private softreference<byte[]> Dataref;public ImageData (String path) {This.path = Path;dataref = new softreference<byte[]> (new Byte[0]);} Private byte[] Readimage () {return new byte[1024 * 1024];//omitted to read file operation}public byte[] GetData () {byte[] DataArray = Datare F.get (); if (DataArray = = NULL | | dataarray.length = = 0) {DataArray = Readimage ();d ataref = new softreference<byte[]> (DataArray);} return DataArray;}}
The-XMX can be used to limit the memory available to the JVM when executing the above program.
Because the object that the soft reference points to may be reclaimed, it is always checked to see if the object is still alive when it obtains the object that the soft reference is actually pointing to through the Get method.
Weak references (weak reference)
Weak references (weak reference) are weaker in strength than soft references. represented by class WeakReference. It does this by referencing an object, but it does not prevent the object from being recycled. Assuming that a strong reference is used, the referenced object cannot be recycled, only if the reference exists. Weak references do not have this problem. At the time the garbage collector executes. Suppose that all references to an object are weak references. The object is recycled.
The function of weak references is to solve the coupling relationship between objects brought by strong references in the time of survival.
The most common use of weak references is in a collection class, especially in a hash table. The hash table interface agrees to use whatever Java object is used as a key. When a key-value pair is placed into a hash table, the Hashtable object itself has a reference to those keys and value objects. If such a reference is a strong reference, then only the hash table object itself will survive. The key and value objects included are not recycled. Suppose that a very long-lived hash table contains very many key-value pairs. Finally, it is possible to consume all the memory in the JVM.
The solution to such a situation is to use weak references to refer to these objects so that the key and value objects in the hash table can be garbage collected. Weakhashmap is available in Java to meet this common need.
Virtual reference (Phantom Reference)
Before you introduce a virtual reference, you should first describe the object termination mechanism (finalization) provided by Java.
There is a finalize method inside the object class. It was designed to perform some cleanup work before an object is actually recycled. Because Java does not provide the same mechanism as a C + + destructor, it is implemented by the Finalize method. The problem is that the garbage collector's execution time is not fixed, so the actual execution time of these cleanup tasks is unpredictable. A virtual reference (phantom reference) solves the problem. It is necessary to specify a reference queue when creating a virtual reference phantomreference. After the Finalize method of an object has been called. The virtual reference to this object is added to the queue.
By checking the contents of the queue, you know whether an object is ready to be recycled.
In some cases, the program will need to be notified when an object's accessibility has changed. For example, a strong reference to an object is no longer present, leaving only soft or weak references. But there is also a need to do some processing of the reference itself. A typical scenario is in a hash table.
The reference object is the key object in the Weakhashmap, and when the actual object it references is garbage collected, it is necessary to remove the key pair from the hash table. With the reference queue (Referencequeue), it is convenient to obtain these weakly referenced objects and remove them from the table. Before soft and weak reference objects are added to the queue, their references to the actual objects are voluntarily emptied.
By referencing the queue's Poll/remove method, the reference objects in the queue can be obtained in a non-clogging and congested manner, respectively.
References:
Deep understanding of the JVM
Java Deep Exploration
Java reference types are explained in detail