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-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the citation types in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the citation types in Java"?

Reference types in Java: strong reference, soft reference, weak reference and virtual reference

Strong citation

For example, Object object = new Object (), then object is a strong reference, and if an object has a strong reference, the garbage collector will never recycle it.

Soft reference

Soft references are used to describe objects that are also useful but not necessary. Before a memory overflow is about to occur, these objects are listed in the scope of collection for secondary garbage collection. If there is not enough memory in this collection, the memory overflow will occur.

In addition: soft references can be used to implement memory-sensitive caching.

Weak reference

Used to describe non-essential objects. Objects associated with weak references can only survive until the next garbage collection occurs. When garbage collection occurs, objects associated with weak references are reclaimed regardless of whether the current memory is sufficient or not.

Virtual reference

The weakest citation relationship. An object instance cannot be obtained through a virtual reference; whether an object has a virtual reference or not does not affect its lifetime.

The only purpose of setting a virtual reference is to receive a system notification when garbage collection occurs.

One difference between virtual references and soft and weak references is that virtual references must be used in conjunction with reference queues (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 reference to the reference queue associated with it before garbage collection. The program knows whether the referenced object will be garbage collected by determining whether the virtual reference has been added to the reference queue. If the program has found that the virtual reference has been added to the reference queue, it will take the necessary action before the memory of the referenced object is reclaimed.

Analysis of the usage scenario of soft reference and weak reference: to achieve memory-sensitive cache.

Thank you for your reading, the above is the content of "what are the reference types in Java". After the study of this article, I believe you have a deeper understanding of which reference types in Java, 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