Tuesday, July 20, 2010

Some causes of memory leaks in JAVA

· Collection classes, such as hashtables and vectors, are common places to find the cause of a memory leak. This is particularly true if the class has been declared static and exists for the life of the application

· Failure to remove Stale Object References from Data Structures

· A reference to a short-term object is used transiently by a long-term object but the long-term object does not clear the reference when it’s done with it

· When the user register a class as an event listener without bothering to unregister when the class is no longer needed

o If you register an object, do also unregister

o If you add Observers to Observables, think also about remove them

o If you install actions to GUI components, do also deinstall.

· Member variables of a class that point to other classes not to be set to null at the appropriate time

· Do never build caches without limits

· Do never build pools without limits

· There are cases (ThreadLocal, for instance) where other objects indirectly reference the class loader causing it to remain uncollected

· If the finalizer method throws an error or exception, a memory leak will occur

· Via not properly closing the objects that needs to be closed like ResultSet,Stream etc

· If you use WeakHashMap, the key object must not be strongly reachable by the value object

No comments:

Post a Comment