- 强引用 StrongReference
- 弱引用 WeakReference
- 软引用 SoftReference
- 虚引用 PhantomReference
引用类型 GC时JVM内存充足 GC时JVM内存不足 强引用 不被回收 不被回收 弱引用 被回收 被回收 软引用 不被回收 被回收
ThreadLocal中用于保存变量的Map,是ThreadLocal的静态内部类,使用弱引用保存数据
static class ThreadLocalMap {
static class Entry extends WeakReference<ThreadLocal<?>> {
/** The value associated with this ThreadLocal. */
Object value;
Entry(ThreadLocal<?> k, Object v) {
super(k);
value = v;
}
}
参考文档
极客时间 弱引用 自动更新 vfs 软引用做缓存