[Java] Reference Object
时间:2010-09-13 来源:web_surf
- Documents
- http://www.ibm.com/developerworks/library/j-refs/
- http://www.cnblogs.com/abob/archive/2009/10/27/1590711.html
- http://download-llnw.oracle.com/javase/1.3/docs/api/java/lang/ref/package-summary.html
- Package
java.lang.ref
- Objective
If you want to refer to some objects without preventing VM to collect them, you can use reference object. For example, in the statement "String obj = new String("data");", GC will not collect obj unless it goes out of scope or it is assigned to null; but if you use reference object as following:
String obj = new ("data");
WeakReference ref = new WeakReference(obj); //Create a reference to obj, the reference doesn't prevent VM to collect obj
obj = null; //Inform GC to collect obj at some time
if (ref.get() == null) //You can refer to obj with ref, but first of all, you must check if obj is collected
{
System.out.println("obj is not collected");
}
else
{
System.out.println("obj is collected");
}
There are 3 types of reference:
- PhantomReference
- WeakReference
- SoftReference
- xxx
相关阅读 更多 +