fix weak bug

This commit is contained in:
Looly
2022-04-10 18:30:07 +08:00
parent 477657ffb8
commit af977ac3d4
11 changed files with 106 additions and 122 deletions

View File

@@ -1,6 +1,9 @@
package cn.hutool.core.util;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.mutable.MutableObj;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.lang.ref.PhantomReference;
@@ -31,4 +34,23 @@ public class ReferenceUtilTest {
// get方法永远都返回nullPhantomReference只能用来监控对象的GC状况
Assert.assertNull(integerReference.get());
}
@Test
@Ignore
public void gcTest(){
// https://blog.csdn.net/zmx729618/article/details/54093532
// 弱引用的对象必须使用可变对象不能使用常量对象比如String
WeakReference<MutableObj<String>> reference = new WeakReference<>(new MutableObj<>("abc"));
int i=0;
while(true){
if(reference.get()!=null){
i++;
Console.log("Object is alive for {} loops - ", i);
System.gc();
}else{
Console.log("Object has been collected.");
break;
}
}
}
}