当前位置:网站首页>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 .
边栏推荐
- Open the field of maker education and creation
- ambari SSLError: Failed to connect. Please check openssl library versions.
- 窗帘做EN 1101易燃性测试过程是怎么样的?
- Detailed explanation of iptables firewall rules and firewalld firewall rules
- 品达通用权限系统(Day 5~Day 6)
- 从零到一,教你搭建「以文搜图」搜索服务(一)
- Problems with cat and dog queues
- A queue of two stacks
- RT-Thread 双向链表(学习笔记)
- ELK 搭建日志分析系统 + Zipkin服务链路追踪整合
猜你喜欢

GenICam GenTL 标准 ver1.5(2)

The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?

Pinda general permission system (day 5~day 6)

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

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

Tiktok actual battle ~ take off the blogger

TFTLCD display experiment of mini plate based on punctual atom stm32

Web APIs DOM event foundation dark horse programmer

The operating mechanism of spectrogram in audio Science

Introduction to SQLSERVER database
随机推荐
GO语言-select语句
AspNetCoreRateLimit 速率限制 接口访问限制 限流控制
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Meichuang data security management platform has obtained the evaluation certificate of "data security product capability verification plan" of the Institute
抖音实战~取关博主
Several important physical concepts
GenICam GenTL 标准 ver1.5(2)
有大佬出现过mysql cdc用 datastream时,出现重复binlog消息的情况吗
Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
视频爆炸时代,谁在支撑视频生态网高速运行?
Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
11_ Deliberate practice and elaboration
Chapter 1 Introduction to bash
first. Net core MVC project
品达通用权限系统(Day 5~Day 6)
测试/开发程序员真的是青春饭吗?世界是公平的,咱们都凭实力说话......
利用ELK 搭建日志分析系统(二)—— 部署安装
用一个栈实现另一个栈的排序
Reading notes of top performance version 2 (II) -- CPU monitoring