|
@@ -0,0 +1,28 @@
|
|
|
+package com.sf.jvm.ref;
|
|
|
+
|
|
|
+import java.lang.ref.SoftReference;
|
|
|
+
|
|
|
+public class SoftRefDemo {
|
|
|
+
|
|
|
+ static class DataBlock{
|
|
|
+ private final byte[] bytes;
|
|
|
+
|
|
|
+ public DataBlock(int byteCount){
|
|
|
+ this.bytes = new byte[byteCount];
|
|
|
+ }
|
|
|
+
|
|
|
+ public String toString(){
|
|
|
+ return "DataBlock { byteCount = " + bytes.length + "}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 1024 * 1024 * 10 = 10MB
|
|
|
+ SoftReference<DataBlock> softRef = new SoftReference<DataBlock>(
|
|
|
+ new DataBlock(1024 * 1024 * 8));
|
|
|
+ // 获取软引用对象
|
|
|
+ System.out.println(softRef.get());
|
|
|
+ byte[] other = new byte[1024 * 1024 * 8];
|
|
|
+ System.out.println(softRef.get());
|
|
|
+ }
|
|
|
+}
|