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 references in java?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What are the four citations in java? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Each language has its own way of manipulating memory elements, C through pointers and java through references. As an object-oriented language, everything is an object in java. But the identifier of our operation is actually a reference of the object. Today we're going to analyze four references in java.

I. the history of citation

In Java, our garbage collection mechanism collects garbage objects based on their references. For example, through different garbage collection algorithms, there are two kinds: reference counting and reachability analysis.

1, reference counting method: add a reference counter for each object, every time a reference points to it, the counter increases 1, when the reference expires, the counter minus 1, when the counter is 0, it is considered that the object can be recycled.

2, reachability analysis algorithm: search downward from an object called GC Roots. If an object is not linked to GC Roots with any reference chain, the object is not available.

At present, garbage collection basically adopts the second way, and the first method has been basically abandoned.

Before jdk1.2, java had only two states of reference: "referenced" and "unreferenced". Later, after JDK.1.2, Java extended the concept of reference into four categories: strong reference, soft reference, weak reference and virtual reference, which is the topic we are talking about today. The intensity of these four citations decreases in turn.

Two and four kinds of citations

1. Strong quotation

If an object has a strong reference, it will not be reclaimed by the garbage collector. Even if the current memory space is insufficient, JVM does not reclaim it, but instead throws an OutOfMemoryError error, causing the program to terminate abnormally. For example, String str = "hello" when str is a strong reference. Of course, we can also use str=null to cancel a strong reference. Let's test the strong reference with the code:

If we first set some jvm parameters,-Xms2M-Xmx3M means that the initial memory is 2m and the maximum memory is 3m. The steps for setting up are as follows:

Steps:

1. Select the project that has already been written

2. Double-click Run- > Debug configurations- > Java Application

3. Arguments- > VM arguments

4. The memory parameters of the virtual machine can be set in VM arguments.

5. After the configuration is completed, Apply- > Debug

1public class Test {

2 public static void main (String [] args) {

3 testStrongReference ()

4}

5 private static void testStrongReference () {

6 / / 1m is normal, but OutOfMemoryError will appear when we switch to 3m.

7 byte [] buff = new byte [1024 * 1024 * 3]

8}

9}

2. Soft reference

If an object has a soft reference, it will not be reclaimed by the garbage collector. Soft references are reclaimed by the garbage collector only when there is insufficient memory space. Such references are often used to implement caching techniques. Because the contents of the cache are emptied later when there is insufficient memory. Let's test it again: the premise is to set the virtual machine parameter-Xms2M-Xmx3M.

1private static void testSoftReference () {

2 / / create 10 bytes with a size of 1m and assign values to soft references

3 for (int I = 0; I < 10; iTunes +) {

4 byte [] buff = new byte [1024 * 1024]

5 SoftReference sr = new SoftReference (buff)

6 list.add (sr)

7}

8 / / notify JVM actively and recommend recycling

9 System.gc ()

10 / / cycle through

11 for (int iTuno; I < list.size (); iTunes +) {

12 Object obj = (SoftReference) list.get (I) .get ()

13 System.out.println (obj.toString ())

14}

15}

We can test it and find that only the last soft reference object is printed, and all the others are null. Why is that? This is because the parameter we set is a maximum of 3m, and when we run out of memory, we will actively collect the garbage of these objects.

3. Weak citation

If an object has a weak reference, the weak reference will be reclaimed once a weak reference object is found during garbage collection, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, weak reference objects may not be found quickly. If we change the above objects to weak references, you will find that all of them are empty, which is because 10 1m objects are created, exceeding 3m, regardless of whether there is enough memory or not.

4. Virtual reference

If an object has a weak reference, it is equivalent to no reference and can be recycled at any time. Virtual references are references created using PhantomReference. Virtual references, also known as ghost references or phantom references, are the weakest of all reference types. Whether an object has a virtual reference has no effect on its life cycle at all, and it is impossible to obtain an object instance through a virtual reference.

The purpose of using virtual references is to know when the object is GC, so virtual references can be used for some operations before destruction, such as resource release, etc. This virtual reference is completely imperceptible to the object, whether it is exactly the same, but for the user of the virtual reference, it is like the pulse line of the object to be observed, through which you can observe whether the object has been recycled or not. in order to deal with it accordingly.

After reading the above, have you mastered what the four references in java are? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report