With regard to Java references, you must know these

Source: Internet
Author: User

Reference

The Java virtual machine takes over all the memory allocation and recycling work, greatly reducing the programmer's workload and error rate. When GC reclaims memory, it usually uses an algorithm called accessibility analysis to determine whether an object can be recycled. In the accessibility analysis, the reference of the object has a decisive role. In, the GC starts from GC Roots down the reference chain to find objects that currently have references to object 1, Object 2, Object 3, Object 4, and Object 5, Object 6, Object 7, although there are references to each other but cannot be Part of the reference. As a result, object 1-4 in the figure is the surviving objects, while object 5-7 is a recyclable.

Although GC can complete garbage collection work, it still cannot avoid out of memory. On the one hand, in the development process you need to be aware that references that are no longer used are set to NULL to free the referenced objects, and you need to consider using appropriate reference types to better manage object memory from the perspective of object references.

Types of references

Java has 4 types of references: Strong (strong reference), soft (soft reference), weak (weak reference), and Phantom (virtual Reference).

Strong Reference: a strong reference is a normal reference in Java. Any time we create a new object, a strong reference is created by default. For example, a static variable of a class, which has been allocated memory since the class was initialized, cannot be reclaimed by GC as a strong reference object, waits for the virtual machine to exit or the class is unloaded to free the reference from being reclaimed by GC.

Weak references : Weak references cannot guarantee that an object must survive in memory, and objects associated with a weak reference can only survive until the next garbage collection occurs. We can use the WeakReference class to implement weak references.

soft References: Soft references are a little stronger than weak references, garbage collection occurs when weak references are bound to be reclaimed, and soft references request that the GC retain itself unless there is no other choice, it can be understood that the GC will only reclaim soft references if a memory overflow is to occur. We can use the SoftReference class to implement soft references.

Virtual Reference : Whether an object has a virtual reference exists, does not have an impact on its lifetime, and cannot obtain an object instance through a virtual reference. The only purpose of setting a virtual reference association for an object is to be able to receive a system notification when the object is reclaimed by GC. We can use the Phantomreference class to implement a virtual reference. When the GC is ready to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory. Developers can determine whether an object is about to be recycled by judging whether the reference queue contains objects, and can do some processing before recycling.

Reference Queue : If a reference is associated with a reference queue, the GC reclaims the object's memory and adds the reference to the reference queue. When a reference queue contains references, it means that references to objects in the heap memory are recycled.

Referenced apps

Building the cache

Using soft references can be used to create a Java local cache, as long as the memory is still sufficient for the cache to be deleted, and once the memory tension is about to overflow, the GC will delete some of the cache to free up memory. For example, creating a data object that is time-consuming to affect performance, a resource that can be reused for a while, infrequently changing data, and so on, can use soft references to build a cache that guarantees read performance without causing memory overflow.

Weakhashmap

Weakhashmap is a hash table implemented with weak reference keys. When a key in Weakhashmap is no longer used by a strong reference, the GC recycles this key the next time garbage is reclaimed. After the keys in the Weakhashmap are reclaimed, the entries for the hash table are also recycled by the GC. Therefore, Weakhashmap can be used to temporarily store some objects that do not need to be used for a long time, which effectively avoids memory overflow.

Summary

The most common references in Java development are strong references, which are strongly referenced by new creation objects. A strong reference prevents the GC from releasing object memory, which can cause memory overflow for long periods of time. In development, you should set the value to null after the strong reference variable is used to help GC garbage collection. There are three other reference types, weak references, soft references, and virtual references in Java. Weak references and soft references can be used to build caches and avoid memory leaks, and virtual references can be used to inform and process objects that will be recycled.

The comparison of the four reference types is as follows:

The original address: About Java references, you must know these

With regard to Java references, you must know these

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.