"J2SE" This is the Java Virtual Reference (phantomreference)

Source: Internet
Author: User

First, Java.lang.ref

The JAVA.LANG.REF package specification (see what is described in the JDK API) is important in a few points.

1. The classes in this package allow the application to interact with the JVM garbage collector;

2. The accessibility of references is gradually reduced: strong references, weak references, soft references, virtual references;

3. Automatically clear references: only weak references and soft references will automatically clear the reference (this indicates that the virtual reference is not automatically purged), the time to clear is to register the reference to the reference queue (only if the memory is not enough, the soft reference will be recycled, regardless of memory enough, as long as garbage collection, the weak reference will be recycled);

4. A virtual reference does not automatically clear the reference, and when a virtual reference is considered certain to be reclaimed by the garbage collector, the virtual reference is registered to the reference queue and not to the reference queue until the garbage collector is recycled, as soft references and weak references must be.


Solution:

1. Verifying the automatic removal of weak and soft references;

2. Side Description Virtual Reference registration time-what is bound to be reclaimed by the garbage collector;


Second, the verification preparation1. Introduction to verification methods

A. Select method isenqueued in Java.lang.ref.Reference to verify that the current reference has entered the reference queue; Clear logic also needs to know a method, Enqueue, which puts the current reference into the queue and modifies the current reference queue as Referencequeuee Nqueued, the whole principle is simple, the rest of the code is not posted out.


B. When a reference has entered the queue, You can use the method poll in the call Java.lang.ref.ReferenceQueue to refer to the queue, especially when a reference to the queue, his own associated reference queue is once again modified (the last time to enter the queue), the whole idea can be so, "We are friends when I'm not mad (reference and reference queues are dependencies), and I'm going to mess with you (the first time you are modified in the queue, the references are no longer dependent on the reference queue), and you don't know who (the second time out of the queue)", in fact, there is no need for reference in the Dequeue, However, the reference queue may also be dependent on other references, so the out-of-queue reference is explicitly reference=null.


2. Validation class Introduction

Prepares a class that does not override the Finalize method, overrides the Finalize method, and overrides the Finalize method to produce this escape.

3. Knowledge paving

This article deals with a little knowledge supplement to garbage collection.

In the Deep Java Virtual machine (Zhou Zhiming) book, the Garbage Collection algorithm "Accessibility analysis" (used to state what objects are garbage collected), when an object is considered to be unreachable (up to, recoverable, not up to three states), the object is garbage reclaimed. The JVM determines that an object is unreachable, that there is no strong reference to the object, and that it is necessary to call the Finalize method of the object (only once in an object's life cycle, remember that the method will only be called once), because if the Finalize method in object is overridden, The overriding logic may cause the object to become accessible, and the object will not be recycled.

Whether it is necessary to call the Finalize method of the object, when the object's class does not override object's Finalize method, or if the object's Finalize method has been called, the JVM considers it unnecessary to call, and the object is immediately considered unreachable; a slightly more complicated situation is that When a class of an object overrides the Finalize method of object and has not been called, the object is placed in a low-priority queue called F-queue, waiting for the Finalize method to be executed, and if the Finalize method is executed, the object is still unreachable. The object enters the unreachable state, waits for garbage collection, and if the Finalize method is executed and the object becomes reachable (the Finalize method has a problem), the object is not garbage collected and the object becomes reachable.

Third, Phantomreference1. Auto Clear

Do not rewrite the Finalize method can be, as long as there is no finalize escape, memory must be recycled, but it is important to note that if you use the string class to do the validation class, you may not get the results you want, Because the existence of a string pool mechanism built into the JVM causes the existence of string Chiqiang references and is therefore not garbage collected, it is recommended that other classes be used as validation classes.

String does the test and the custom class do the test results, as shown in the string object is not garbage collected and therefore is not queued.



2. Virtual ReferencesA. Do not rewrite the Finalize method

2. Overriding the Finalize method


3. When the Finalize method occurs this escape


Four, this is a virtual referenceUnlike soft references, weak references that automatically reclaim memory, the existence of virtual references (although memory is still recycled) tends to send notifications when an object is determined to be recycled (the reference object in the virtual reference does not determine whether the memory has been reclaimed, and the soft and weak references must be reclaimed memory). will send a notification (into the queue and out of the queue) to the application, "I'm going to be cleaned up, do you want to do something about it?" ”。 Therefore, the virtual reference is used to do the object cleanup, better than the Finalize method, will not cause the garbage collector to do extra work, with the reference blocking method remove can be more timely cleanup.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"J2SE" This is the Java Virtual Reference (phantomreference)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.