Android内存泄漏分析及实践(二)
通过《Android内存泄漏分析及实践(一)》我们得到结论:可以通过查询ReferenceQueue是否包含WeakReference实例,判断弱引用指向的对象 o是否被系统回收。所以根据这个理论我们可以写一个简单的内存泄露分析工具。
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
public class CheckWhenAnObjectIsNoLongerReferenced {
private static Object object;
public static void main(String[] args) {
object = new Object();
// Reference queue, to which registered reference objects are appended
// by the
// garbage collector after the appropriate reachability changes are
// detected.
ReferenceQueue<Object> rq = new ReferenceQueue<Object>();
// Create a new weak reference that refers to the given object and is
// registered with this queue.
WeakReference<Object> wr = new WeakReference<Object>(object, rq);
// start a new thread that will remove all references of object
new Thread(runnable).start();
// wait for all the references to the object to be removed
try {
Reference<?> r = null;
while ((r = rq.remove()) != null) {
if (r == wr) {
System.out.println("Object is no longer referenced.");
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println("Setting object to null");
object = null;
System.out.println("Running Garbage Collection...");
Runtime.getRuntime().gc(); // run GC to collect the object
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
Output:
Setting object to null
Running Garbage Collection…
Object is no longer referenced.