看下图,明明执行gc了,为啥还是能访问到内容?
我加上了 final、static 都试过了,gc 依然没清除掉

看下图,明明执行gc了,为啥还是能访问到内容?
我加上了 final、static 都试过了,gc 依然没清除掉

关注让【宝码香车】来帮你解答,本回答参考DeepSeek编写,并整理提供。
如果还有疑问可以点击头像关注私信或评论。如果答案让您满意,请采纳、点赞、关注,非常感谢!
若您愿意支持,欢迎直接打赏,将不胜感激!
关于ThreadLocal的key使用弱引用但GC无法回收的问题,关键在于理解ThreadLocal的内存模型和引用关系。以下是详细分析:
public class ThreadLocalTest {
private ThreadLocal<String> threadLocal = new ThreadLocal<>();
public static void main(String[] args) throws InterruptedException {
ThreadLocalTest test = new ThreadLocalTest();
test.threadLocal.set("123");
System.gc(); // 手动触发GC
Thread.sleep(1000);
System.out.println(test.threadLocal.get()); // 仍然能输出"123"
}
}

public static void main(String[] args) {
ThreadLocal<String> tl = new ThreadLocal<>();
tl.set("value");
tl = null; // ① 解除强引用
System.gc();
// 此时Entry的key会被回收,但value仍在内存中(需手动remove)
}
threadLocal.remove()public class CorrectUsage {
private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();
public static void main(String[] args) {
try {
threadLocal.set("data");
// 使用threadLocal...
} finally {
threadLocal.remove(); // 必须清理
}
}
}
关键结论:弱引用key的设计是为了防止ThreadLocal对象本身的内存泄漏,但value的清理仍需开发者负责。GC不回收是因为当前线程的ThreadLocalMap仍持有Entry的强引用,且ThreadLocal对象本身仍被强引用。