当前位置:网站首页>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 .
边栏推荐
- 《性能之巅第2版》阅读笔记(二)--性能观察工具
- leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
- 设计一个有getMin功能的栈
- Learning notes of digital circuit (II)
- Component splitting practice
- 品达通用权限系统(Day 5~Day 6)
- 多项目开发入门,基础设计 类库项目使用
- ELK 搭建日志分析系统 + Zipkin服务链路追踪整合
- Building a server monitoring platform with telegraf influxdb grafana
- 等保三级密码复杂度是多少?多久更换一次?
猜你喜欢

Chapter 1 Introduction to bash

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

品达通用权限系统(Day 5~Day 6)

设计一个有getMin功能的栈

leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】

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

English grammar_ Adjective / adverb Level 3 - Comparative_ Useful Expressions

Tiktok actual battle ~ take off the blogger

Leetcode: monotonic stack structure (Advanced)

Pinda general permission system (day 5~day 6)
随机推荐
Zipkin 服务链路追踪
Open the field of maker education and creation
Introduction notes to machine learning
Several important physical concepts
等保2.0密码要求是什么?法律依据有哪些?
Conversion between decimal and BCD codes in C language
Reading notes of top performance version 2 (II) -- Performance observation tool
Pychart shares third-party modules among different projects
One article tells you what kubernetes is
Tiktok actual battle ~ take off the blogger
机器学习入门笔记
English grammar_ Adjective / adverb Level 3 - Comparative_ Useful Expressions
27年,微软IE结束了!
美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书
Reverse a stack with recursive functions and stack operations only
Secouer le son et se battre ~ prêter attention au blogueur
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?
Introversion, lying flat and midlife crisis
Multi project design and development · introduction to class library project
04 MongoDB各种查询操作 以及聚合操作总结