Four references in Java

Source: Internet
Author: User

There are four types of references in Java:

1. strongreference)

Strong references are the most common references. If an object has a strong reference, the garbage collector will never recycle it. When the memory space is insufficient, the Java Virtual Machine would rather throw an outofmemoryerror to terminate the program abnormally, and does not recycle strongly referenced objects at will to solve the problem of insufficient memory.

2. Soft reference)

If an object only has soft references, the memory space is sufficient and the garbage collector will not recycle it. If the memory space is insufficient, the memory of these objects will be recycled. The object can be used by programs as long as the garbage collector does not recycle it. Soft references can be used to implement memory-sensitive high-speed cache.

Soft references can be used together with a referencequeue. If the referenced objects are recycled by the garbage collector, the Java virtual machine adds this soft reference to the reference queue associated with it.

3. weakreference)

The difference between weak references and soft references is that only objects with weak references have a shorter life cycle. When the Garbage Collector thread scans the memory area under its jurisdiction, its memory will be recycled no matter whether the current memory space is sufficient or not. However, since the garbage collector is a thread with a low priority, it may not soon find objects with weak references.

Weak references can be used together with a referencequeue, the Java virtual machine adds this weak reference to the reference queue associated with it.

4. phantomreference)

As the name implies, "virtual references" are just the same as virtual ones, which are different from other types of references. Virtual references do not determine the object lifecycle. If an object only holds a virtual reference, it is the same as no reference and may be recycled by the garbage collector at any time.

Virtual references are used to track the activity of objects recycled by the garbage collector. A difference between virtual and soft references and weak references is that virtual references must be used together with the reference Queue (referencequeue. When the garbage collector is preparing to recycle an object, if it finds that it has a virtual reference, it will add this virtual reference to the reference queue associated with it before it recycles the object's memory.

Summary:

Both weakreference and softreference can be used to save instance references of objects. These two classes are related to garbage collection.
Weakreference is a weak reference, in which the stored object instance can be recycled by GC. This class is usually used to save the object reference somewhere without interfering with GC collection of the object. It is usually used in programs such as debug and memory monitoring tools. This type of program generally requires that the object be observed and does not affect the normal GC process of the object.
Recently, weakrefrence is also found in the JDK proxy class implementation code. The proxy will save the dynamically generated class instance to a map consisting of weakrefrence as the cache.

Softreference is a strongly referenced object instance. Unless JVM is about to outofmemory, it will not be recycled by GC. This feature makes it especially suitable for designing object cache. For cache, it is best to keep the cached object in the memory. However, if the JVM memory is tight, in order to avoid system crash caused by outofmemoryerror, the JVM is allowed to reclaim the cache memory when necessary, the data will be re-loaded to the cache when appropriate. In this way, the system can be designed to be more flexible.

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.