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

Sample analysis of references and Threadlocal

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Quote and Threadlocal example analysis, I believe that many inexperienced people do not know what to do about this, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1 background

One day, a group friend in a certain group suddenly asked a question: "the key of threadlocal is a weak reference, so in threadlocal.get (), after the occurrence of GC, is key a null?" In front of the screen, you can think about this question. Here, I'll talk about the quotes in Java and ThreadLocal.

2 references in Java

For many Java beginners, references are confused with objects. Here is a piece of code

User zhangsan = new User ("zhangsan", 24)

Let's start with the question: is zhangsan a reference or an object? Many people will think that zhangsan is an object, and if you think so, then take a look at the following code

User zhangsan

Zhangsan = new User ("zhangsan", 24)

This code is actually the same as the original code. The first line of this code, User zhangsan, defines zhangsan. Do you think zhangsan is still an object? If you still think so, what should this object be? Indeed, zhangsan is really just a reference. Students who are familiar with JVM memory partition should be familiar with the following picture:

In fact, zhangsan is a reference allocated in the stack, while new User ("zhangsan", 24) is an object allocated in the heap. The function of'='is to point references to objects in the heap. Just like your name is Zhang San, but Zhang San is not a real person, he just points to you.

What we generally call references are actually strong references. There are more than one kind of references after JDK1.2. Generally speaking, there are four kinds of references: strong references, soft references, weak references, and virtual references. And then I'll introduce these four citations one by one.

2.1 strong citation

We've talked about this.

User zhangsan = new User ("zhangsan", 24); this is a strong reference, a bit like a C pointer. To strongly quote him, there are the following characteristics:

Strong references can access the target object directly.

As long as the object is associated with a strong reference, the garbage collector will not collect it, even if it throws an OOM exception.

Easily lead to memory leaks.

2.2 soft references

Using SoftReference in Java helps us define soft references. There are two construction methods:

Public SoftReference (T referent)

Public SoftReference (T referent, ReferenceQueue k, Object v) {

Super (k)

Value = v

}

}

You can see that Entry is a subclass of WeakReference, and the object associated with this weak reference is our ThreadLocal object. Let's go back to the above question:

"the key of threadlocal is a weak reference, so in the case of threadlocal.get (), after GC occurs, is key a null?"

At a glance at this problem, weak references and garbage collection must be null, which is actually wrong, because the title is talking about doing threadlocal.get () operation, which proves that there are still strong references. So key is not null. If our strong reference does not exist, then Key will be recycled, that is, our value will not be recycled, key will be recycled, causing value to exist forever, resulting in memory leaks. This is why ThreadLocal is often reminded of the need for remove () by many books.

You may ask if you see a lot of source ThreadLocal that doesn't write remove and still uses it very well. That's because a lot of source code often exists as static variables with the same life cycle as Class, and remove needs to use ThreadLocal in those methods or objects, because strong references are lost due to the destruction of the method stack or objects, resulting in memory leaks.

3.2 FastThreadLocal

FastThreadLocal is a high-performance native thread replica variable tool provided in Netty. Many powerful tools are provided in Netty's io.netty.util, which will be introduced one by one later, so let's talk about FastThreadLocal first.

FastThreadLocal has the following characteristics:

Use arrays instead of ThreadLocalMap to store data for faster performance. (there will be no hash conflict between the cache line and the first location)

Due to the use of arrays, there is no Key recycling, value is not recycled, so memory leaks are avoided.

After reading the above, have you mastered the method of citation and sample analysis of Threadlocal? 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