|
Hi list,
I'm playing around with a container that holds weak references
to the contained objects and that also has some information associated
to them. From the comment it seems that an EphemeronDictionary was just
the perfect match. Now, one of the things that I need to do is to throw
an announcement each time an object of the container is garbage
collected, which is pretty simple by setting the container as the
dictionary manager. So far, so good.
Now, the problem is how to write a unit test that actually verifies that
the event is thrown; I tried with things like:
testObjectGCed
| newObject oid |
newObject:=Object new.
self container add: newObject.
self
whenExecuting: [
newObject:=nil.
ObjectMemory garbageCollect.
]
theObject: self container
shouldAnnounce: ObjectGarbageCollected
but the test sometimes passes and sometimes it doesn't. I tried
different approaches (like delay/force gc loops) to see if the object is
finally collected, but nothing. What I find specially weird is that if I
use a special dictionary, implemented with my own weak associations (not
of "indexedType: #ephemeron" but just using a weak array) the test never
fails. As a matter of fact when I load 20 or 30 objects in the
container I can even close the image (saving it, of course) and re-open
it and some ephemerons are still there and it takes a couple of manual
GC for them to die.
So, my doubts are:
1. Why is that ephemerons take so long to be GCed while weak references
are removed the first time I force a GC?
2. Is there a way to force the VM to clear all ephemerons? At least to
make sure I can write a test case that validates the container behaviour.
Thanks in advance,
Andrés
|