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

Source: Internet
Author: User
First, Java.lang.ref

JAVA.LANG.REF Package specification (see JDK API for content, this article is not posted) a few points are important.

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

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

3. Automatically clear references: only weak references and soft references automatically clear the reference (this indicates that the virtual reference is not automatically cleared), the time to purge is to register the reference to the reference queue (soft references are recycled only if there is not enough memory; If garbage collection occurs, the weak references will be reclaimed);

4. A virtual reference does not automatically clear the reference, and when a virtual reference is considered to be recycled by the garbage collector, the virtual reference is registered in the reference queue, not as a soft reference or a weak reference must wait for the garbage collector to recycle before it is registered to the reference queue;

5. An object is associated with a virtual reference that does not affect its lifecycle at all, although it does not automatically clean up like a soft reference or weak reference, but its memory is reclaimed and when it is recycled is equivalent to a reference relationship that does not exist (as with a virtual reference association), and is completely unaffected;


Solve:

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

2. Side Description Virtual Reference Registration Opportunity-what is bound to be recycled by the garbage collector;


Second, the verification preparation 1. Introduction to verification methods

A. Select the 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, that is, Enqueue, which puts the current reference in the queue and modifies the currently referenced queue as Referencequeuee Nqueued, the whole principle is very simple, the rest of the code will not be posted.


B. When a reference has entered the queue, You can use the method in the call Java.lang.ref.ReferenceQueue to poll the reference out of the queue, especially when a reference out of the queue, his own associated reference queue will be modified again (the last time to enter the queue), the whole idea can be so, "When you don't annoy me, we're friends. (References and reference queues are dependencies), I'm messing with you. (The first time the queue is modified, the reference is no longer dependent on the reference queue), finish you who do not know who (when the second change in the queue)", in fact, quote in the column when there is no need to exist, However, the reference queue may also be dependent on other references, so a reference to the queue is explicitly reference=null.


2. Certification 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 Matting

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

In the book Deep Java Virtual machine (Zhou Zhiming), the garbage collection algorithm "Accessibility analysis" (used to state what objects will be garbage collected), when an object is considered to be unreachable (up to, recoverable, up to three states), the object is garbage collected. The JVM determines that an object is unreachable, one is that the object does not have a strong reference, and the second is whether it is necessary to call the Finalize method of the object (only once in an object's lifecycle, remember that the method is only invoked once), because if you rewrite the Finalize method in object, It is possible in the rewrite logic to make the object accessible, and the object will not be retrieved or reclaimed.

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

Third, Phantomreference 1. Automatic removal

No rewriting of the Finalize method is possible, as long as no finalize escape occurs, the memory will 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 the built-in string pooling mechanism in the JVM causes the existence of string Chiqiang references and is therefore not garbage collected, it is recommended that you use other classes as validation classes.

string as a test result of testing and custom classes, as shown in the following illustration, the string object is not garbage collected and therefore will not be added to the queue.



2. Virtual Reference A. Do not rewrite the Finalize method

2. Rewrite the Finalize method


3. When the Finalize method occurs this escape


Four, this is the virtual reference is not like soft references, weak references automatically reclaim memory, and the presence of a virtual reference (although memory is still reclaimed) is more likely to send notifications when an object is determined to be reclaimed (at which point the referenced object in the virtual reference is not sure whether the memory has been reclaimed). Soft references and weak references must be reclaimed memory, and a notification is sent to the application (into the queue and out of the queue), "I'm going to be cleaned up, are you going to do something?"     ”。 Therefore, the virtual reference used to do 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 clean-up.

Related Article

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.