System.gc not enqueuing cleared WeakReferences

I

Ian Pilcher

I have (finally!) reached the point of writing unit tests for my
WeakIdentityHashMap, and testing for proper removal of otherwise
unreachable keys is proving to be quite a problem.

Currently, my tests assume that a call to System.gc() will ensure that
all cleared WeakReferences will be enqueued, but this does not appear to
be the case.

Does this sound credible? (This is Sun's 1.5.0_06 JVM on Linux, BTW.)

If System.gc() won't do the trick, does anyone know of anyway to ensure
that all WeakReferences are actually enqueued?

TIA
 
I

Ian Pilcher

Thomas said:
References are not guaranteed to be enqueued immediately after they are
cleared. In Sun's implementation all references get passed through a
maximum priority thread before becoming enqueued. A further complication
with UNIX threading is that, unless the process is run as root, the
thread priority is apparently ignored.

I was afraid of that. I know that we're not supposed to depend on the
behavior of the garbage collector, but this non-determinism makes
testing things a real PITA.
I have a suspicion that References objects that are dropped themselves
or only referenceable via WeakReferences do not get enqueued. That was
on my list of things to check...

The doc is pretty explicit that you are correct.
 
T

Thomas Hawtin

Ian said:
I have (finally!) reached the point of writing unit tests for my
WeakIdentityHashMap, and testing for proper removal of otherwise
unreachable keys is proving to be quite a problem.

Currently, my tests assume that a call to System.gc() will ensure that
all cleared WeakReferences will be enqueued, but this does not appear to
be the case.

Does this sound credible? (This is Sun's 1.5.0_06 JVM on Linux, BTW.)

References are not guaranteed to be enqueued immediately after they are
cleared. In Sun's implementation all references get passed through a
maximum priority thread before becoming enqueued. A further complication
with UNIX threading is that, unless the process is run as root, the
thread priority is apparently ignored.
If System.gc() won't do the trick, does anyone know of anyway to ensure
that all WeakReferences are actually enqueued?

I have a suspicion that References objects that are dropped themselves
or only referenceable via WeakReferences do not get enqueued. That was
on my list of things to check...

Tom Hawtin
 
M

Mike Schilling

Ian Pilcher said:
I was afraid of that. I know that we're not supposed to depend on the
behavior of the garbage collector, but this non-determinism makes
testing things a real PITA.

I have found it useful in testing such things to create lots of large
objects (say, 100,000-integer arrays) in a loop, which seems to force enough
collection to drive reference-enqueuing and even finalization. There are
still no guarantees, but you might give it a whirl.
 
T

Thomas Hawtin

Mike said:
I have found it useful in testing such things to create lots of large
objects (say, 100,000-integer arrays) in a loop, which seems to force enough
collection to drive reference-enqueuing and even finalization. There are
still no guarantees, but you might give it a whirl.

The observable effects are quite possibly because that acts as a delay
loop rather than it forcing more GC.

Tom Hawtin
 
A

Adam Maass

Ian Pilcher said:
I have (finally!) reached the point of writing unit tests for my
WeakIdentityHashMap, and testing for proper removal of otherwise
unreachable keys is proving to be quite a problem.

Currently, my tests assume that a call to System.gc() will ensure that
all cleared WeakReferences will be enqueued, but this does not appear to
be the case.

Does this sound credible? (This is Sun's 1.5.0_06 JVM on Linux, BTW.)

If System.gc() won't do the trick, does anyone know of anyway to ensure
that all WeakReferences are actually enqueued?

AFAIK, the specification does not require that gc *immediately* enqueue
cleared weak references -- only that they be enqueued sometime after they
are, in fact, cleared. That may be the source of your troubles.

-- Adam Maass
 
I

Ian Pilcher

Thomas said:
The observable effects are quite possibly because that acts as a delay
loop rather than it forcing more GC.

One way or another, it seems to be doing the trick right now. Since its
testing code, I can live with it for now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top