The Artima Developer Community
Sponsored Link

Desinging for garbage collection

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:

Desinging for garbage collection

Posted by Zak Sy on 02 Jan 1999, 10:29 PM

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
garbage collected were not. It appeared that the classes were not being garbage collected because listeners were not being cleared. However, I found it interesting that the classes
that were not being collected were not in scope. For example, I had the the following:

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
{
private B myB;

...Class A stuff

public void prepareForGc()
{
// Code to remove listeners and any other weak references
// Then call any other member's prepareForGc()
myB.prepareForGc();
}
}

public class B
{
private C myC;

...Class B stuff

public void prepareForGc()
{
// Code to remove listeners and any other weak references
// Then call any other member's prepareForGc()
myC.prepareForGc();
}
}

public class C
{
private C myC;

...Class C stuff

public void prepareForGc()
{
// Code to remove listeners and any other weak references
// Then call any other member's 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:
  • Hmm... Bill Venners 13 Jan 1999, 1:59 PM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us