Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
I am working on a large java application that has started to suffer from memory leakeage. Upon using a profiling tool I noticed that some classes that I thought should be getting Class A contains a member of type Class B. Class A registers itself as a listener of Class B. Likewise, Class B contains a member of Class C, and Class B registers itself as a listener of Class C. When I was done with an instance of class A, I assumed all I needed to do to make my instance of class A and it's members candidates for garbage collection was to set my instance of class A to null. However, what I observed was that my instance of class A was garbage collected, but member instances of class B and C were left in memory. To eliminate this I was forced in to the following design. public class A ...Class A stuff public void prepareForGc() public class B ...Class B stuff public void prepareForGc() public class C ...Class C stuff public void prepareForGc() Each class was responsible for removing listeners and other weak references inside of it's prepareForGc() method. Furthermore, each class needed to call any member's prepareForGc() method to ensure that each appropriate class down the chain was made ready for garbage collection to avoid possible memory leaks. In the article on finalization and cleanup one of the suggestions was to create a new method instead of overriding finalize() to handle cleanup. Is my usage of prepareForGc() what the article was suggesting, or have I missed the boat somewhere? This design seems like it could lead to maintenance problems down the road. Is there a better way to get rid of this type of memory leak? Should the JVM do a better job of garbage collecting in this type of scenario? All help and/or advice is highly appreciated. Regards, Zak.
Replies:
|
Sponsored Links
|