Hold references in Java

Source: Internet
Author: User

Thinking in Java is not clear about this place. I have not read many articles on the Internet.

It means you want to access an object in the future, but now you want to recycle it first, you can use a reference to implement it. The problem is that recycling is recycling, how can I access it again? No one said.

After searching for information, I found that the above information was incorrect!

The role of holding references is to allow you to continue holding (possibly) References to recycled objects after GC execution, and clear the memory for the image instance, hold references provide limited interaction with the garbage collector.

Garbage collection Overview

Before the Garbage Collector recycles an object, it must determine whether the object is alive. Java uses the root search algorithm to determine whether the object is alive. The basic idea is to use a series of objects named GC roots as the starting point to search down from these nodes. When an object cannot be reached by any GC roots, the object is considered recoverable. However, you also need to pay attention to the important details: the collection of objects alone does not mean that the garbage collector can recycle it at a specified operation. Due to different garbage collection algorithms, some algorithms analyze objects with shorter lifetime more frequently than older objects with longer lifetime. Therefore, an object that can be collected may never be recycled. This situation may occur if the program ends before the Garbage Collector releases the object. Therefore, in summary, you will never guarantee that the objects available for collection will always be collected by the garbage collector.

Some terms:

  • Stronugly reachable ):Objects that can be accessed through strong references.
  • Softly reachable ):Objects that are not highly accessible and can be accessed through soft references.
  • Weakly reachable ):Objects that are neither strong or soft or accessible through weak references.
  • Phantomly reachable ):It is not a strong object, soft object, or weak object. It has ended and can be accessed through virtual reference.
  • Clear:Set the referent field of the referenced objectnullAnd declare the object referenced by the reference class in the heap
    Endable
    .

The Java. Lang. Ref package provides three classes for three different reference types:

Softreference class

SoftReferenceA typical use of class is for memory-sensitive high-speed cache.
SoftReferenceThe principle is to ensure that all soft references are cleared before the JVM reports memory insufficiency when the object is referenced. The key is that the Garbage Collector may (or may not) release software and objects at runtime. Whether the object is released depends on the garbage collector algorithm and the amount of memory available when the garbage collector is running.

Weakreference class

WeakReferenceA typical use of a class is canonicalized mapping ). In addition, for objects with relatively long lifetime and Low Re-creation overhead, weak references are also useful. The key is that if the spam collector encounters a weak object during runtime, it will be released
WeakReferenceThe referenced object. However, note that the spam Collector may have to run multiple times to locate and release weak accessible objects.

Phantomreference class

PhantomReferenceClass can only be used to track the upcoming collection of referenced objects. It can also be used for pre-mortem cleanup.
PhantomReferenceMust beReferenceQueueClass. YesReferenceQueueIt can act as a notification mechanism. When the Garbage Collector determines that an object is virtual and accessible,
PhantomReferenceThe object is placed in it.ReferenceQueue. SetPhantomReferencePut the object in
ReferenceQueueIt is also a notification, indicatingPhantomReferenceThe object referenced by the object has ended and can be collected. This allows you to take action just before the memory occupied by the object is recycled.

The above two points need to be noted. One is that softreference is used for high-speed cache, which can be understood as maintaining a group of dynamic memory, objects in this memory need to be recycled by GC without strong references pointing to them. What is the normalization ing? It means that you can use weakreference to create a group of mappings, and the objects in this group of mappings can be recycled. For example, when we create a map normally, we can add objects to it, the added objects are reachable by their container map, so they cannot be recycled. If the objects are encapsulated by weakreference and then added to the map, they can be recycled, please refer to the specific here: http://c2.com/cgi/wiki? Canonicalizedmapping.

The following describes the interaction with the garbage collector and how to use it with reference: Java 2 reference class user guide example.

Use reference API:

The reference objects API allows programs to interact with the garbage collectorhttp: // www.javaworld.com/javaworld/109-01-2002/109-0104-java101.html? Page = 2

In addition, Java provides weakhashmap to implement standard ing, which achieves the same effect as the implementation using weakreference. It is worth noting that the key must be recycled again before it can be recycled for the first time) and value (second recycling) are recycled.

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.