Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four references in Java

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the four kinds of references in Java". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are the four kinds of references in Java" together!

First, since JDK 1.2, references to objects have been divided into four levels, giving programs more flexibility over the life cycle of objects. These four levels are, from high to low, strong references, soft references, weak references, and virtual references.

strong reference

Strong references are the most commonly used. If an object has strong references, it will never be GC. For example:

Object strongReference = new Object();

When out of memory, the JVM would rather throw OutOfMemoryError, causing the program to terminate abnormally, than solve the out-of-memory problem by arbitrarily reclaiming objects with strong references.

If the strongly referenced object is not used, it needs to be weakened so that it can be GC, for example, the clear() method in ArrayList:

/** * Removes all of the elements from this list. The list will * be empty after this call returns. */ public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }

By explicitly setting a strongly referenced object to null or beyond the object's lifetime, the garbage collector will reclaim the object as if it has no references. Exactly when it is collected depends on the specific garbage collector.

soft references

If an object has only soft references, the garbage collector will not collect it when there is enough memory space; if there is not enough memory space, the garbage collector will collect the memory of these objects. As long as the garbage collector doesn't collect it, the object can be used by programs. Let's look at an example to understand specifically:

String str = new String("abc"); SoftReference softReference = new SoftReference(str); String result = softReference.get();

Let's look at get():

public T get() { T o = super.get(); // timestamp represents the last time the soft reference was used (initialization, get()) // clock represents the time of the last GC if (o != null && this.timestamp != clock) this.timestamp = clock; return o; }

Therefore, when soft references are garbage collected, they also follow the LRU rule, giving priority to the least recently used objects for collection.

Soft references are mostly used in memory-sensitive caches. Specifically, we want to store data in a cache so it can be read quickly. However, when there is not enough memory in the JVM, we do not want cached data to consume JVM memory. For example, in conjunction with ReferenceQueue, if the object referenced by the soft reference is garbage collected, the JVM will add the soft reference to the reference queue associated with it:

ReferenceQueue referenceQueue = new ReferenceQueue(); String str = new String("abc"); SoftReference softReference = new SoftReference(str, referenceQueue); str = null; // Notify GC System.gc(); System.out.println(softReference.get()); // abc Reference

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report