Java holds references & Weakhashmap

Source: Internet
Author: User

Java holds references & Weakhashmap

@author Ixenos

    • Reference referencing objects

      • available (reachable): means that there is a normal reference in the stack pointing to this object, or there may be more intermediate links

        • If an object is available, the garbage collector (GC) cannot release it because he is still used by the program, preferring to run out of memory and not release it, and when an object is not available, the program cannot use it, and the GC considers it safe to recycle it.

      • Reference object: If you want to continue to hold a reference to an object, but also want to be able to allow the GC to release it, you should use the Reference object as the medium between the normal reference (proxy), that is, the generic reference is wrapped by the reference object and points to an object

      • Normal reference-->reference Object---target object

    • Reference four categories of referenced objects (from strong to weak)

      • Strong reference (Strong Reference): When a strong reference is a normal reference, the object is available (reachable)

      • Soft reference (Soft Reference): Implement memory-sensitive cache//todo

      • Weak reference (Weak Reference): Implements a canonical mapping (canonicalizing mappings), which does not prevent GC from reclaiming the mapped key or value, the canonical mapping Instances of objects in a program can be used simultaneously in many places to save storage space
    • Phantom Reference (Phantom Reference): Used to schedule cleanup before recycling, which is more flexible than the Java termination mechanism
      Phantom references are different from weak and soft references. It controls that the object it points to is very weak (tenuous) so that it cannot get the object. The get () method typically returns a null value. Its only function is to track the dead objects that are queued in the referencequene. The difference between a phantom reference and a weak reference is the way in which the queue (Enquene) enters Referencequene. When the weakly referenced object becomes reachable, the weak reference is queued into referencequene. This queue occurs before the end (Finialize) and garbage collection actually occurs. Theoretically, the object of being garbage can be revived (resurrected) through the irregular (unorthodox) finilize () method, but the weak reference is still dead. Phantom references are queued only when the object is physically moved out of memory. This prevents us from re-restoring the object that will die. W: End (finalization) refers to a more general concept of rubbish recycling, which can reclaim arbitrary resources that the object occupies, such as file descriptors and graphics contexts. Phantom references are made up of two benefits: A: It is the only way to determine when an object is removed from memory. Typically, this is not very useful. But sooner or later (come in handy) uses the case of manual processing of large pictures: If you're sure that a picture needs to be garbage collected, you should wait for the photo to be recycled before you try to load the next photo. This makes the dreaded (dreaded) memory overflow unlikely to occur. B: The fundamental problem of the unreal reference being able to avoid the end (finilize). The Finilize () method can be resurrected by associating a strong reference to a garbage object. The problem is that objects that overwrite the Finilize () method have to perform two separate loops for recycling before they become garbage. The first round of loops determines that an object is garbage, and it conforms to the condition of terminating finilize. This object may be "resurrected" during the finilize process. The garbage collection period has to be rerun before the object is actually removed. Because Finilization is not called in real time, multiple loops of the GC may occur during termination. This leads to some delay lag when actually cleaning up the garbage objects. This causes a large amount of garbage in the heap to cause a memory overflow. Phantom references cannot happen above, and when the Phantom reference is queued, it has actually been removed from memory. Phantom memory cannot "revive" the object. When this object is found to be unreal, it is recycled in the first round of the loop. It can be proved that the Finilize () method is never used in the first case, but unreal references provide a more secure and efficient use and exclusion of finilizemethod to make garbage collection easier. But because there are so many things to do, I usually don't use finilize.
      Phantom Reference Principle

    • Referencequeue Reference Queue
      • With soft references and weak references , you can choose whether or not to put them into referencequeue (used as a tool for "clean up before recycling"), while Phantom references are recycled into referencequeue

      • The difference between a Phantom reference and a weak reference is the way in which the queue (Enquene) enters Referencequene:

        • When the weakly referenced object becomes reachable, the weak reference is queued into referencequene. This queue occurs before the end (Finialize) and garbage collection actually occurs. Theoretically, the object of being garbage can be revived (resurrected) through the irregular (unorthodox) finilize () method, but the weak reference is still dead, because no reference points to it;

        • Phantom references are queued only when the object is physically moved out of memory. This prevents us from re-restoring the object of death;

        • End (finalization) refers to a more general concept of rubbish recycling, which can reclaim arbitrary resources that the object occupies, such as file descriptors and graphics contexts.

      • Take a weak reference as an example

        • Once a weak reference returns a null value, the object it points to becomes garbage, and the weak reference object is useless. This usually means cleaning up (cleanup) in a certain way. For example, Weakhashmap will remove some of the dead (dread) entry and avoid having too many weak references to die. Referencequene can easily track down these dead weak references;

        • You can pass referencequene into the constructor of Weakhashmap (constructor) so that if the object pointed to by the weak reference becomes garbage, the weak reference will be added to the Referencequene

    • Weakhashmap Weak hash mapping table
      • Weakhashmap using weak references (weakreference) to save keys, called weak keys

      • Ref-->weak Ref-->key, if (Ref==null), than (weak ref-->ref queue, Weakhashmap will delete the corresponding entry)

      • Steps for Dynamic recycling :

(1) new weakhashmap, adding "key value pair" to Weakhashmap

When you add a "key-value pair" to a weakhashmap, the keys you add are weak keys;

In fact, Weakhashmap is a entry (key-value pair) saved by an array table; Each entry is actually a one-way list, that is, the entry is a list of key-value pairs;

(2) when a "weak key" is no longer referenced by another object and is recycled by GC. When the "weak key" is reclaimed by the GC, the "weak key" is also added to the queue

For example, when we add a "weak key" key to Weakhashmap, we later set the key to null. At this point, there is no external external object to reference the key;

Then, when the GC of the Java Virtual Machine reclaims memory, the relevant memory of key is reclaimed, and the key is added to the queue.

(3) the next time we need to manipulate weakhashmap, the table and queue are synchronized first. All of the key-value pairs are saved in the table, and the "weak keys" that are collected by GC are saved in the queue, and they are deleted by deleting the key-value pairs corresponding to the "weak keys" retrieved by GC in the table.

For example, when we "read an element in a weakhashmap or get the size of a weakreference", it synchronizes the table and queue first to "delete the key-value pairs corresponding to the ' weak keys ' that are collected by GC in the table." The method of deletion is to compare the ' key ' of the elements in table and the ' key ' in the queue, and if they are equivalent, delete the key-value pair in table.

-

Java holds references & Weakhashmap

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.