当前位置:网站首页>Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal
Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal
2022-06-28 04:14:00 【smartjiang-java】
List of articles
1:VarHandle
The last one said AQS Source code , There is also a knowledge point VarHandle, Look at the code below 
Everyone will ask. :VarHandle What is it? ? What's the role ?
Actually :VarHandle yes jdk1.9 After that , Represents a reference to a variable , Atomic thread - safe operations can be performed through cas Method to compare the set value . Faster than reflection , Direct manipulation of binary code , Reflection should be checked before each operation
2: Strong weak virtual reference
Part of the content of the blog quotes :http://blog.csdn.net/liuxian13183
2.1. Strong citation
Object object=new Object(); that object Is a strong reference . If an object has Strong citation , It's similar to the necessities , The garbage collector will never recycle it . When memory runs out of space ,Java The virtual machine would rather throw OutOfMemoryError error , Causes the program to terminate abnormally , It will not solve the problem of insufficient memory by randomly recycling objects with strong references .
2.2. Soft citation (SoftReference)
If an object has only Soft citation , It's similar to the things you can have for daily use . If there's enough memory , The garbage collector won't recycle it , If there's not enough memory , It will reclaim the memory of these objects . As long as the garbage collector doesn't recycle it , This object can be used by the program . Soft citation It can be used to implement memory sensitive cache . Soft references can be combined with a reference queue (ReferenceQueue) A combination of , If the object referenced by the soft reference is garbage collected ,Java The virtual machine adds the soft reference to the reference queue associated with it .
2.3. Weak reference (WeakReference)
Usually used in containers WeakHashMap also ThreadLocal . If an object has only weak references , It's similar to the things you can have for daily use . The difference between weak quotation and soft quotation is : Objects with only weak references have a shorter life cycle . In the process of the garbage collector thread scanning the memory area it governs , Once you find an object with only weak references , Whether the current memory space is enough or not , Will reclaim its memory . however , Because the garbage collector is a low priority thread , So it's not necessarily easy to find objects with only weak references . Weak references can be associated with a reference queue (ReferenceQueue) A combination of , If the object referenced by the weak reference is garbage collected ,Java The virtual machine will add this weak reference to the reference queue associated with it .
2.4. Virtual reference (PhantomReference): Manage off heap memory
" Virtual reference " seeing the name of a thing one thinks of its function , It's the same thing , Different from other quotations , Virtual references do not determine the life cycle of an object . If an object only holds virtual references , Then it's the same as without any reference , Can be recycled at any time . Virtual references are mainly used to track the garbage collection activities of objects . One of the differences between virtual reference and soft reference and weak reference is : Virtual references must be with reference queues (ReferenceQueue) A combination of . When the garbage collector is ready to recycle an object , If it's found to have virtual references , Before reclaiming the object's memory , Add this virtual reference to the reference queue associated with it . The program can judge whether a virtual reference has been added to the reference queue , To see if the referenced object is going to be garbage collected . If the program finds that a virtual reference has been added to the reference queue , Then you can take the necessary action before the memory of the referenced object is recycled .NIO Inside PirectByteBuffer Point to off heap memory , It is called direct memory , Managed by the operating system ,GC Cannot recycle directly . So we need to use virtual references , When direct memory needs to be reclaimed , By referencing queue input detection , And then clean it up .C or C++ virtual machine , use delete or free.Java What can be used is UnSafe Class freeMemory() To recycle .
3:ThreadLocal
3.1: Thread local
Other threads cannot access
3.2: principle
set() Source code :Set The current thread's map in , Other threads are unreadable
map Not empty :Thrad.currentThread.map(ThreadLocal,valve)
Map It's empty : initialization map
3.3:ThreadLocal Use with weak references , Solve the memory leak
ThreadLocal Of map Medium entry Is a weak reference 
1: Solve the memory leak problem : If strong citation , Even if tl=null, but key The reference to still points to ThreadLocal object ,ThreadLocal Cannot be recycled , There will be Memory leak . Using weak references makes key Point to ThreadLocal Object becomes recyclable . Solved the memory leak problem .
2: There is a memory leak problem :ThreadLocal After being recycled ,key The value of the into null, Lead to value Cannot be accessed , There are also memory leaks . therefore , commonly Threadlocal Run out of , manual remove fall .
Be careful : This article only represents the personal views of novice bloggers , If something is wrong or there is a better idea through the technology , Welcome to leave a message , Sharing and communication make us progress , thank you .
边栏推荐
- 猫狗队列的问题
- 品达通用权限系统(Day 5~Day 6)
- Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
- Sorting from one stack to another
- 利用ELK 搭建日志分析系统(三)—— 安全认证
- 美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书
- 2021年终总结及2022年展望
- MSC 307(88) (2010 FTPC Code) Part 9床上用品试验
- After launching the MES system, these changes have taken place in the enterprise
- Open the field of maker education and creation
猜你喜欢

欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?

领歌leangoo敏捷看板工具新增导出卡片文档和粘贴共享脑图节点功能

《性能之巅第2版》阅读笔记(二)--CPU监测

Talking about cloud primitiveness, we have to talk about containers

leetcode - 329. Longest increasing path in matrix

RT thread bidirectional linked list (learning notes)

From zero to one, I will teach you to build a "search by text and map" search service (I)

Staggered and permutation combination formula

27年,微软IE结束了!

Visualization of loss using tensorboard
随机推荐
从零到一,教你搭建「以文搜图」搜索服务(一)
Moonbeam集成Coin98,给予用户在多链道路上的更多选择
first. Net core MVC project
With the transformation of automatic empowerment, Feihe dairy accelerates its move towards digitalization!
11_刻意练习精讲
Single responsibility principle
[small program practice series] e-commerce platform source code and function implementation
27年,微软IE结束了!
【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
02 mongodb data types, important concepts and common shell instructions
GO语言-select语句
2021 year end summary and 2022 outlook
抖音实战~取关博主
MySQL master-slave replication, separation and resolution
Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)
GCD maximum common divisor
Building log analysis system with elk (II) -- deployment and installation
applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
公司领导说,个人代码超10个Bug就开除,是什么体验?