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 reference types in Java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what are the citation types in Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the reference types in Java"?

Brief introduction

Starting from the JDK1.2 version, the references of objects are divided into four levels, so that the program can control the life cycle of objects more flexibly. The four levels from high to low are strong reference, soft reference, weak reference and virtual reference, which are described below.

Strong citation

A strong reference is the most commonly used reference type. As shown below, new Object () creates an Object object and stores it on the heap, and the variable object stores a strong reference to that object.

Object object = new Object ()

Strong references are not garbage collected, so if you want to recycle the object, you should set the display of variables pointing to the object to null, so that the object changes from strong reference to no reference.

Example:

Public class ReferenceDemo {public static void main (String [] args) throws IOException {/ / strong references will not be garbage collected ReferenceDemo referenceDemo = new ReferenceDemo (); / / strong references will be converted to no references, and no references can be garbage collected referenceDemo = null; / / trigger garbage collection System.gc () / / block the main thread and wait for the garbage collection thread to execute System.in.read ();} / / call @ Override protected void finalize () throws Throwable {super.finalize (); System.out.println ("- finalize-");}} soft reference before the object is collected

Soft references are created using SoftReference, which will not be recycled when there is sufficient memory space, but will be recycled before the virtual machine throws the OutOfMemoryError when there is insufficient memory space.

Example:

Public class ReferenceDemo {public static void main (String [] args) throws InterruptedException {/ / create a soft reference to the ReferenceDemo object SoftReference softReference = new SoftReference (new ReferenceDemo ()); / / trigger garbage collection System.gc (); / / block the main thread, waiting for the garbage collection thread to execute Thread.sleep (5000) / / softReference.get () returns a soft reference object, or null System.out.println (softReference.get ()) if the object has been garbage collected; / / creates a 25m byte array byte [] bytes = new byte [1024 * 1024 * 25]; / / runs out of memory, blocks the main thread and waits for the garbage collection thread to execute Thread.sleep (5000) / / re-output the soft reference object System.out.println (softReference.get ());}} / * * output result: * com.buhe.demo.demos.reference.ReferenceDemo@76fb509a * null * /

Note: the heap memory size needs to be set to 30m (- Xmx30m-Xms30m) before the example is run.

Purpose: soft references can be used for memory-sensitive caches, and cached objects are saved until they are recycled because of insufficient memory space.

Weak reference

Weak references are created using WeakReference, and as long as weak references are found during garbage collection thread execution, weak reference objects will be reclaimed regardless of whether there is enough memory space. Because the garbage collection thread is a low-priority thread, objects with only weak references may not be found quickly.

Example:

Public class ReferenceDemo {public static void main (String [] args) throws InterruptedException {/ / create a weak reference to ReferenceDemo WeakReference weakReference = new WeakReference (new ReferenceDemo ()); / / weakReference.get () returns a weak reference object, or null System.out.println (weakReference.get ()) if the object has been garbage collected; / / triggers garbage collection System.gc () / / block the main thread and wait for the garbage collection thread to execute Thread.sleep (3000); / / re-output the weak reference object System.out.println (weakReference.get ());}} / * * output result: * com.buhe.demo.demos.reference.ReferenceDemo@76fb509a * null * /

Purpose: weak references can also be used for caching, you can refer to the WeakHashMap class.

Virtual reference

Virtual references are created using PhantomReference, which is the weakest of all reference types. Virtual reference objects, like objects without references, can be garbage collected at any time, and virtual references must be used with reference queues.

When the garbage collection thread collects a virtual reference object, it destroys the object after garbage collection and adds PhantomReference to the reference queue.

Example:

Public class ReferenceDemo {public static void main (String [] args) throws InterruptedException {/ / create reference queue ReferenceQueue referenceQueue = new ReferenceQueue (); / / create a virtual reference to ReferenceDemo PhantomReference phantomReference = new PhantomReference (new ReferenceDemo (), referenceQueue); / / phantomReference.get () always returns null System.out.println ("phantomReference.get ():" + phantomReference.get ()) / / poll this queue to see if there is a Reference object available, and return it if so, otherwise return null System.out.println ("referenceQueue.poll ():" + referenceQueue.poll ()); / / trigger garbage collection System.gc (); / / block the main thread and wait for the garbage collection thread to execute Thread.sleep (3000) System.out.println ("- after garbage collection -"); System.out.println ("phantomReference.get ():" + phantomReference.get ()); System.out.println ("referenceQueue.poll ():" + referenceQueue.poll ()) }} / * * output result: * phantomReference.get (): null * referenceQueue.poll (): null *-after garbage collection-* phantomReference.get (): null * referenceQueue.poll (): java.lang.ref.PhantomReference@76fb509a * /

Purpose: virtual references can be used to accurately detect when the object is deleted from memory and to determine whether the object has been reclaimed by checking the reference queue.

At this point, I believe you have a deeper understanding of "what are the reference types in Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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