JAVA软引用,使用System.gc()回收引用的对象 5C
 public class SoftReferenceTest {
    private static List<Byte[]> list = new ArrayList<Byte[]>();;
    private static SoftReference<SoftReferenceTest> sr;

    public static void main(String[] args) {
        // 软引用
        // 当内存溢出的时候,应用的对象会被 回收
        sr = new SoftReference<SoftReferenceTest>(new SoftReferenceTest());
        System.out.println("sr : " + sr.get());

        sr= null;
        System.gc();
        // 产生MMO后可以看到finalize的执行,即“软引用,当内存溢出的时候,应用的对象会被 回收”
//        Byte[] bytes = null;
//        for (int i = 0; i < 50000; i++) {
//            bytes = new Byte[1024 * 1024];
//            list.add(bytes);
//        }
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("SoftReferenceTest ... finalize ... exec...");
        System.out.println("sr : " + sr.get());
    }
}

为什么finalize的第二个System.out.println没有打印?

4个回答

package com.pt;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

import org.junit.Test;

public class RefTest {

@Test
public void testStrongRef() {
    User user = new User("pan");
    User strongRef = user;
    user = null;
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        // TODO: handle exception
    }
    System.out.println("Strong: " + strongRef.getUserName());

    //------测试发现,String不会被回收------

// {
// String str = "hello";
// String strongRef = str; //强引用
// str = null;
// System.gc(); //垃圾回收
// try {
// Thread.sleep(1000);
// } catch (Exception e) {
// // TODO: handle exception
// }
// System.out.println("Strong: " + strongRef);
// }

}

@Test
public void testWeakRef(){
    User user = new User("pan");
    WeakReference<User> weakRef = new WeakReference<User>(user);
    System.out.println("weakRef: " + weakRef.get().getUserName());
    user = null;
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        // TODO: handle exception
    }
    //null
    System.out.println("weakRef: " + weakRef.get());


    //-----------测试发现String不会被回收---------

// String str = "hello";
// WeakReference wr = new WeakReference(str);
// System.out.println("weakRef: " + wr.get());
// str = null;
// System.gc();
// System.gc();
// System.gc();
// try {
// Thread.sleep(3000);
// } catch (Exception e) {
// // TODO: handle exception
// }
// System.out.println("weakRef: " + wr.get());
}

@Test
public void testSoftRef(){
    User user = new User("pan");
    SoftReference<User> softRef = new SoftReference<User>(user);
    System.out.println("SoftRef: " + softRef.get().getUserName());
    user = null;
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        // TODO: handle exception
    }
    //非null
    System.out.println("SoftRef: " + softRef.get());


    //-----------测试发现String不会被回收---------

// String str = "hello";
// WeakReference wr = new WeakReference(str);
// System.out.println("weakRef: " + wr.get());
// str = null;
// System.gc();
// System.gc();
// System.gc();
// try {
// Thread.sleep(3000);
// } catch (Exception e) {
// // TODO: handle exception
// }
// System.out.println("weakRef: " + wr.get());
}

}

不要显式调用gc,对性能影响很大

finalize 只会被调用一次

图片说明

周志明版 深入理解java虚拟机

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!