forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftReferenceTest.java
More file actions
35 lines (27 loc) · 877 Bytes
/
SoftReferenceTest.java
File metadata and controls
35 lines (27 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.learnjava.reference;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 软引用
*/
public class SoftReferenceTest {
public static void main(String[] args) throws InterruptedException {
List<Reference> refList = new ArrayList<Reference>();
for (int i = 0; i < 1000; i++) {
// 10mb
byte[] buffer = new byte[1024 * 1024 * 10];
SoftReference softReference = new SoftReference(buffer);
refList.add(softReference);
}
System.gc();
Thread.sleep(1000);
Iterator<Reference> it = refList.iterator();
while (it.hasNext()) {
Reference ref = it.next();
System.out.println("当前ref引用的对象:" + ref.get());
}
}
}