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

How to use four reference types in JAVA Springboot

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

Share

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

Editor to share with you how to use the four citation types in JAVA Springboot, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Concept introduction

Different reference types mainly reflect the different reachable states of objects and their impact on garbage collection.

01. Strong citation

This is the normal object we created ~ when the object is explicitly assigned to null, or when it is not referenced by other living objects, it becomes the target of the garbage collector, waiting to be collected

02. Soft reference

Soft reference (SoftReference), which is reclaimed when there is insufficient memory

such as

After being recycled, null will be printed here instead of Java4ye

03. Weak reference

Weak reference (WeakReference), when the garbage collector carries out garbage collection, it will be collected by the garbage collector regardless of whether the memory is sufficient or not

such as

After being recycled, null will also be printed here instead of Java4ye

04. Virtual reference

Virtual reference (ReferenceQueue), which can also be recycled at any time, acts more like a tag. When an object is recycled, it is not null, but note that whenever you call the get method of a virtual reference, you can only get a null value.

The only purpose of setting a virtual reference association for an object is to receive a system notification when the object is reclaimed by the collector--

Here is an example of http://www.javashuo.com/article/p-zyvdcbhl-nb.html 's article.

User user = new User (1, "Java4ye"); ReferenceQueue userReferenceQueue = new ReferenceQueue (); / / create a virtual reference to the User object PhantomReference phantomReference = new PhantomReference (user, userReferenceQueue); / / remove the strong reference user = null;System.out.println (phantomReference.get ()); / / manually trigger GCSystem.gc (); System.out.println ("GC:" + phantomReference.get ()); 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