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

Analysis of reference queue and virtual reference instance of Java

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

Share

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

This article mainly explains the "Java citation queue and virtual citation case analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java citation queue and virtual citation case analysis" bar!

A little bit of eye contact

The reference queue is represented by the ReferenceQueue class, which is used to hold references to recycled objects. When soft references, weak references, and reference queues are used together, after the referenced object is recycled, the system adds the reference corresponding to the recycled object to the associated reference queue. The virtual reference adds its corresponding virtual reference to its associated reference queue before the object is released, which makes it possible to take action before the object is reclaimed.

A virtual reference cannot get the object it corresponds to.

Second actual combat

1 code

Import java.lang.ref.*;public class PhantomReferenceTest {public static void main (String [] args) throws Exception {/ / create a string object String str = new String ("Crazy Java handout"); / / create a reference queue ReferenceQueue rq = new ReferenceQueue (); / / create a virtual reference to the "Crazy Java handout" string PhantomReference pr = new PhantomReference (str, rq) / / cut off the reference between the str reference and the "Crazy Java handout" string str = null; / / take out the object referenced by the virtual reference, and you can't get the referenced object through the virtual reference, so output null System.out.println (pr.get ()); / / ① / / force garbage collection System.gc (); System.runFinalization () / / after garbage collection, the virtual reference will be placed in the reference queue / / the first reference in the reference queue will be compared with pr System.out.println (rq.poll () = = pr); / / ②}}

2 run

Nulltrue

3 description

Use reference classes to avoid leaving objects in memory during program execution. If the object is referenced as a soft reference, weak reference, or virtual reference, the garbage collector can release the object at will. These reference classes are beneficial if you want to minimize the amount of memory your program takes up during its life cycle.

Thank you for your reading, the above is the content of "Java reference queue and virtual reference example analysis". After the study of this article, I believe you have a deeper understanding of the problem of Java reference queue and virtual reference example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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