The Artima Developer Community
Sponsored Link

Legacy Design Forum
Finalization and Cleanup



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.


WeakHashSet problem

Posted by Simon Gould on September 14, 2001 at 9:00 AM

I have a WeakHashSet containing ThreadGroup keys together with
their values. I have written a JUnit test to check that when they're
garbage collected they are removed from the WeakHashSet.

In the test I define a ThreadGroup:

ThreadGroup tg = new ThreadGroup("test");

and call the following function:

rm.addThreadGroup(tg, new MemoryResource(rm.getAmountAvailable()));

addThreadGroup is as follows...
public void addThreadGroup(ThreadGroup tg, Resource tgResource)
throws DuplicateThreadGroupException, OutOfResourcesException {
// ensure that the ThreadGroup hasn't been added already
if(!threadGroupList.containsKey(tg)) {
// ensure that the amount of resource required by
// the new ThreadGroup is available (OutOfResourcesException
// is thrown otherwise)
threadGroupList.put( tg, tgResource );
} else {
throw new DuplicateThreadGroupException
("ThreadGroup already present");

then my test goes on to set tg to null to allow garbage collection
tg = null;

then I add a further ThreadGroup to the WeakHashSet
force garbage collection and add another one.

rm.addThreadGroup(new ThreadGroup("test"), new MemoryResource(0));
rm.addThreadGroup(new ThreadGroup("test"), new MemoryResource(0));

my flush function is as follows...

public static void flushMemory()
Vector v = new Vector();
int count = 0;
int size = 1048576;
while(size > 1)
for (; true ; count++)
v.addElement( new byte[size] );
catch(OutOfMemoryError bounded){size = size/2;}
v = null; //release for GC
v = new Vector();

The problem is that tg, the first ThreadGroup I added
to the WeakHashSet, isn't removed from it :-(

does anyone know why??


Sponsored Links

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