Not many must be aware of the package java.lang.ref.*, and their impact on Garbage Collectors,
- Strong Reference (Frequently used)
- Soft Reference
- Weak Reference
But the downside is the GC will treat these references low in priority and does cleans it up from the heap, you are bound to see Null Pointer Exceptions. Another alternate is SoftReferences which could fit in very well in most of the business cases and keep your memory foot print clean.
public class WeakRef {
public static WeakReference lst = new WeakReference(new ArrayList());
public static void main(String[] args) {
while (true) {
try {
lst.get().add("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Leaving this article a WeakReference to StrongReferences below,
http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ref/package-summary.html
No comments:
Post a Comment