Home  >  Article  >  Java  >  What are Java reference types?

What are Java reference types?

青灯夜游
青灯夜游Original
2020-07-25 11:52:256800browse

In Java, reference types include: 1. Strong Reference; 2. Soft Reference; 3. Weak Reference; 4. Phantom Reference.

What are Java reference types?

Java reference types

There are generally two types of Java types, namely basic data types and reference data types. Among them, the reference data type refers to the object that needs to be referenced in the storage heap. Reference is a feature of Java object-oriented.

Starting from JDK1.2, the reference types in Java are divided into four types, namely:

①Strong Reference (StrongReference)

②Soft Reference (SoftRefernce)

③Weak Reference(WeakReference)

④Phantom Reference(PhantomReference)

Strong Reference-StrongReference

This kind of reference is usually The most commonly used ones in development, such as Stringstrong=newString("StrongReference"), when an instance object has a strong reference, the garbage collector will not recycle the object. When there is insufficient memory, it would rather throw an OutOfMemeryError exception than recycle the strong reference. The referenced object, because the JVM thinks that the strongly referenced object is the object being used by the user, it cannot tell which one should be recycled. Forcible recycling may cause serious errors in the system.

Soft Reference-SoftReference

If an object only has a soft reference, the JVM will recycle the object only when there is insufficient memory, and will not recycle it in other cases. Soft references can be used in conjunction with ReferenceQueue. When the object of the soft reference is recycled due to insufficient system memory, the JVM will add the soft reference to the associated ReferenceQueue.

ReferenceQueuereferenceQueue=newReferenceQueue();
SoftReferencesoftReference=newSoftReference<>(newBook(),referenceQueue);
Bookbook=softReference.get();
Referencereference=referenceQueue.poll();

When the system memory is insufficient, gc is triggered, the Book will be recycled, and the reference will not be null.

Weak Reference-WeakReference

There are only weakly referenced objects. When the JVM triggers gc, the object will be recycled. Unlike soft references, weak references will be recycled regardless of whether there is insufficient memory. Weak references can be used in conjunction with ReferenceQueue. When the system triggers gc and the soft reference object is recycled, the JVM will add the weak reference to the associated ReferenceQueue. However, the priority of the garbage collector thread is very low. , so weak references may not be recycled quickly. The following is an example of actively triggering gc to verify this conclusion.

ReferenceQueuereferenceQueue=newReferenceQueue();
WeakReferenceweakReference=newWeakReference(newBook(),referenceQueue);
Bookbook=softReference.get();
System.gc();
//Runtime.getRuntime().gc();
Referencereference=referenceQueue.poll();

Of course this cannot be reproduced every time, because we call System.gc() just to tell the JVM that it is time to collect garbage, but it is not certain when it will do it, but as far as I tested It seems that as long as you write System.gc() a few more times, the probability of recurrence is still very high.

Virtual Reference-PhantomReference

If an object only has a virtual reference referencing it, the garbage collector can recycle it at any time. Virtual references are mainly used To track the activity of an object being recycled by the garbage collector, when it is recycled, the JVM will add this weak reference to the associated ReferenceQueue.

Different from soft references and weak references, virtual references must have a ReferenceQueue associated with them. The value obtained through phantomReference.get() is null. Imagine if there is no ReferenceQueue associated with it. What is the value of existence?

PhantomReferencephantomReference=newPhantomReference<>(newBook(),referenceQueue);
Bookbook=phantomReference.get();//此值为null
Referencereference=referenceQueue.poll();

Recommended tutorial: "java tutorial"

The above is the detailed content of What are Java reference types?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn