JAVA memory recycling

Java memory management includes memory allocation and memory management, both of which are automatically completed by the JVM, which brings great convenience and aggravates the work of the JVM, thereby making the Java program run slower.

Basically, the object reference in the JVM memory can be understood as a directed graph. The reference variables and objects are regarded as the vertices of the directed graph, and the reference relationship is regarded as the directed edge of the graph. The directed edge always points from the reference end to the target. Reference object. Because objects are created by threads, you can think of thread objects as the starting vertex of a directed graph.

When a heap object is running in memory, it can be divided into three states:

① Reachable state: When an object is created, one or more variables refer to it. In the directed graph, the object can be navigated from the starting vertex, then it is in the reachable state, and the program can call the methods and properties of the object by referencing the variables.

②Recoverable state: If an object no longer has any reference variable to refer to it, it will enter the recoverable state first, and the system's garbage collection mechanism is ready to reclaim the memory occupied by the object. Before recycling, the system will call the finalize method of the recoverable object to clean up resources. If the system calls the finalize method to re-reference the object with one or more reference variables, the object will become reachable again.

③ When all the associations of the object are cut off, and the system calls the finalize method of all objects and still does not make the object reachable, then the object will permanently lose its reference and be recycled by the system.

object state transition

In order to better manage object references, Java has provided three classes under the java.lang.ref package since JDK1.2: SoftReference, PhantomReference, WeakReference, which represent three reference methods of objects: soft reference, virtual reference, Weak references.

To sum up, Java has four reference methods: strong reference, soft reference, virtual reference, and weak reference.

①Strong reference: This is the most common reference method in Java programs. The program creates an object and assigns the object to a reference variable. This variable is a strong reference. Strongly referenced objects will not be garbage collected, no matter how tight the memory is, even if some Java objects will never be used later. Strong references are one of the main causes of Java memory leaks since the JVM will definitely not recycle strongly referenced Java objects.

②Soft references: Soft references need to be implemented through the SoftReference class. When an object only has soft references, it may be forcibly reclaimed by the garbage collection mechanism. For objects with only soft references, when the system memory space is sufficient, it will not be reclaimed by the system, and the program will also use the object. When the system runs out of memory, the system reclaims it.

③Weak reference: Weak reference is similar to soft reference, the difference is that the object referenced by weak reference has a shorter lifetime. Weak references are implemented through the WeakReference class. When the system garbage collection mechanism runs, regardless of whether the system memory is sufficient, the memory occupied by the object will always be reclaimed. Instead of being reclaimed immediately, it will not be reclaimed until the system garbage collection mechanism runs. Similar to the WeakReference function is the WeakHashMap. When the program has a large number of objects that need to be referenced with weak references, you can consider using the WeakHashMap to save them.

④Virtual reference: The virtual reference cannot be used alone. The main function is to track the status of the object being garbage collected. The program can know whether the referenced object is about to be recycled by checking whether the reference queue associated with the virtual reference already contains the specified virtual reference. . The reference queue is represented by the ReferenceQuene class, which is used to hold references to objects after being reclaimed. When soft references, weak references and reference queues are used in combination, after the soft references and weak references are released, their corresponding references will be added to the associated reference queue. Its corresponding phantom reference is added to its associated reference queue, which allows the object to take action before it is freed.

Guess you like

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