- 软引用
内存敏感缓存:当内存不足时才会被回收
比弱引用”强”:JVM会尽量保留软引用对象
1 | // 创建软引用 |
- 弱引用
短期缓存:只要发生GC就会被回收
不会阻止回收:比软引用更脆弱
1 | // 创建弱引用 |
- 虚引用
内存敏感缓存:当内存不足时才会被回收
比弱引用”强”:JVM会尽量保留软引用对象1
2
3// 创建软引用
SoftReference<byte[]> softRef =
new SoftReference<>(new byte[1024 * 1024]); // 1MB