System.gc not enqueuing cleared WeakReferences

Discussion in 'Java' started by Ian Pilcher, Jan 18, 2006.

  1. Ian Pilcher

    Ian Pilcher Guest

    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

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Jan 18, 2006
    #1
    1. Advertising

  2. Ian Pilcher

    Ian Pilcher Guest

    Thomas Hawtin wrote:
    > 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.

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Jan 18, 2006
    #2
    1. Advertising

  3. Ian Pilcher wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 18, 2006
    #3
  4. "Ian Pilcher" <> wrote in message
    news:...
    > Thomas Hawtin wrote:
    >> 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 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.
     
    Mike Schilling, Jan 18, 2006
    #4
  5. Mike Schilling wrote:
    >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 19, 2006
    #5
  6. Ian Pilcher

    Adam Maass Guest

    "Ian Pilcher" <> wrote:
    >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
     
    Adam Maass, Jan 19, 2006
    #6
  7. Ian Pilcher

    Ian Pilcher Guest

    Thomas Hawtin wrote:
    >
    > 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.

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Jan 19, 2006
    #7
  8. "Thomas Hawtin" <> wrote in message
    news:43cee11c$0$1496$...
    > Mike Schilling wrote:
    >>
    >> 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.


    Could be.
     
    Mike Schilling, Jan 19, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Indbond
    Replies:
    10
    Views:
    547
    John C. Bollinger
    Apr 23, 2004
  2. Paul J. Lucas

    Write-to-disk cache via WeakReferences?

    Paul J. Lucas, Jul 23, 2005, in forum: Java
    Replies:
    6
    Views:
    453
    Paul J. Lucas
    Jul 25, 2005
  3. Paul J. Lucas
    Replies:
    2
    Views:
    390
  4. bsobaid
    Replies:
    1
    Views:
    890
    Vladyslav Lazarenko
    Apr 6, 2009
  5. Bryan Balfour
    Replies:
    5
    Views:
    135
    Bryan Balfour
    Jul 28, 2005
Loading...

Share This Page