posted 18 years ago
In Java, typically memory leak occurs when an object of a longer lifecycle has a reference to objects of a short life cycle. This prevents the objects with short life cycle being garbage collected. The developer must remember to remove the references to the short-lived objects from the long-lived objects. Objects with the same life cycle do not cause any issues because the garbage collector is smart enough to deal with the circular references.
--Design applications with an object�s life cycle in mind, instead of relying on the clever features of the JVM. Letting go of the object�s reference in one�s own class as soon as possible can mitigate memory problems. Example: myRef = null;
--Unreachable collection objects can magnify a memory leak problem. In Java it is easy to let go of an entire collection by setting the root of the collection to null. The garbage collector will reclaim all the objects (unless some objects are needed elsewhere).
--Also, take care, while writing singletons, because they are long lived objects.