Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix typo #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/java/java-concurrent.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public class ThreadLocalDemo {

### ThreadLocal内存泄漏的原因?

每个线程都有⼀个`ThreadLocalMap`的内部属性,map的key是`ThreaLocal`,定义为弱引用,value是强引用类型。垃圾回收的时候会⾃动回收key,而value的回收取决于Thread对象的生命周期。一般会通过线程池的方式复用线程节省资源,这也就导致了线程对象的生命周期比较长,这样便一直存在一条强引用链的关系:`Thread` --> `ThreadLocalMap`-->`Entry`-->`Value`,随着任务的执行,value就有可能越来越多且无法释放,最终导致内存泄漏。
每个线程都有⼀个`ThreadLocalMap`的内部属性,map的key是`ThreadLocal`,定义为弱引用,value是强引用类型。垃圾回收的时候会⾃动回收key,而value的回收取决于Thread对象的生命周期。一般会通过线程池的方式复用线程节省资源,这也就导致了线程对象的生命周期比较长,这样便一直存在一条强引用链的关系:`Thread` --> `ThreadLocalMap`-->`Entry`-->`Value`,随着任务的执行,value就有可能越来越多且无法释放,最终导致内存泄漏。

解决⽅法:每次使⽤完`ThreadLocal`就调⽤它的`remove()`⽅法,手动将对应的键值对删除,从⽽避免内存泄漏。

Expand Down