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 ways of citing Java?

2025-01-16 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 ways of citing Java". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the four ways to quote Java?"

1. Strong reference (StrongReference)

Use the most common citation.

As long as the reference chain is not broken, the strong reference is not broken. -when there is insufficient memory space, throwing an OutOfMemoryError Terminator will not reclaim objects with strong references.

Weakens the reference by setting the object to null so that it is recycled

Object object = new Object (); String str = "scc"; / / are strong references 2. Soft reference (SoftReference)

The object is in a useful but not necessary state

Only when there is insufficient memory space, GC reclaims the memory of the referenced object.

Can be used to implement caching-such as web caching, picture caching

/ / Note: the reference wrf is also a strong reference, which points to the object SoftReference. / / the soft reference here refers to the reference to new String ("str"), that is, TSoftReference wrf = new SoftReference (new String ("str")) in the SoftReference class; 3. Weak reference (WeakReference)

A weak reference is that as soon as the JVM garbage collector finds it, it will be reclaimed.

Non-essential objects are weaker than soft references

Will be returned when GC

The probability of being reclaimed is also low, because the GC thread has a lower priority.

Suitable for objects where references are occasionally used and do not affect garbage collection:

Map activeEngineResources = new HashMap (); / / ResourceWeakReference weak reference 4. Virtual reference (PhantomReference)

Does not determine the life cycle of the object.

Can be recycled by the garbage collector at any time

Tracking the activity of an object being reclaimed by a garbage collector, acting as a sentinel

Must be used in conjunction with the reference queue ReferenceQueue

When the garbage collector is ready to recycle an object, if it is found that it has a virtual reference, it will add the virtual reference to the reference queue associated with it.

The program can know whether the referenced object is going to be garbage collected by determining whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is reclaimed.

Object obj = new Object (); ReferenceQueue queue = new ReferenceQueue (); PhantomReference reference = new PhantomReference (obj, queue); / / strong reference object is empty, leaving soft reference obj = null;5. Reference queue (ReferenceQueue)

There is no actual storage structure, and the storage logic is expressed by the relationship between internal nodes.

Stores associated soft references, weak references, and virtual references by GC

At this point, I believe you have a deeper understanding of "what are the four ways of citing 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