Garbage collector question

Discussion in 'Java' started by Vincent Cantin, Sep 23, 2004.

  1. If there is no references exept a WeakReference to an object and I call
    System.gc(), am I sure that my object has been garbage collected ?
     
    Vincent Cantin, Sep 23, 2004
    #1
    1. Advertising

  2. Vincent Cantin wrote:
    > If there is no references exept a WeakReference to an object and I call
    > System.gc(), am I sure that my object has been garbage collected ?
    >


    If a get() call on the WeakReference returns null, the object has been GC'd.

    BK
     
    Babu Kalakrishnan, Sep 23, 2004
    #2
    1. Advertising

  3. > > If there is no references exept a WeakReference to an object and I call
    > > System.gc(), am I sure that my object has been garbage collected ?

    >
    > If a get() call on the WeakReference returns null, the object has been

    GC'd.

    It doesn't answer my question.
     
    Vincent Cantin, Sep 23, 2004
    #3
  4. On Thu, 23 Sep 2004 19:42:08 +0800, Vincent Cantin wrote:
    >> > If there is no references exept a WeakReference to an object and I call
    >> > System.gc(), am I sure that my object has been garbage collected ?

    >>
    >> If a get() call on the WeakReference returns null, the object has been

    > GC'd.
    >
    > It doesn't answer my question.


    There is no guarantee that gc will collect every object that is
    available to be collected, so the answer to your question is no.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 23, 2004
    #4
  5. Vincent Cantin

    xarax Guest

    "Gordon Beaton" <> wrote in message
    news:...
    > On Thu, 23 Sep 2004 19:42:08 +0800, Vincent Cantin wrote:
    > >> > If there is no references exept a WeakReference to an object and I call
    > >> > System.gc(), am I sure that my object has been garbage collected ?
    > >>
    > >> If a get() call on the WeakReference returns null, the object has been

    > > GC'd.
    > >
    > > It doesn't answer my question.

    >
    > There is no guarantee that gc will collect every object that is
    > available to be collected, so the answer to your question is no.


    System.gc() is not guaranteed to perform any collections.
    So, the first answer is the correct response to your original
    question. The only way to know that the instance was collected
    is when WeakReference.get() returns null (or watch for the
    WeakReference to be enqueued on the ReferenceQueue).
     
    xarax, Sep 23, 2004
    #5
  6. Vincent Cantin

    Will Hartung Guest

    "Vincent Cantin" <> wrote in message
    news:...
    > If there is no references exept a WeakReference to an object and I call
    > System.gc(), am I sure that my object has been garbage collected ?


    I'm curious why you care? Why is it important one way or the other?

    If you're using a WeakReference, you've already communicated to the system
    that your interest in the object is temporal based on the whim of the JVM
    and the GC, so why are you interested in the specific behavior? How does
    this affect your applicaton?

    Regards,

    Will Hartung
    ()
     
    Will Hartung, Sep 23, 2004
    #6
  7. > > If there is no references exept a WeakReference to an object and I call
    > > System.gc(), am I sure that my object has been garbage collected ?

    >
    > I'm curious why you care? Why is it important one way or the other?
    >
    > If you're using a WeakReference, you've already communicated to the system
    > that your interest in the object is temporal based on the whim of the JVM
    > and the GC, so why are you interested in the specific behavior? How does
    > this affect your applicaton?
    >
    > Regards,
    >
    > Will Hartung
    > ()


    because I need to know in some points of given class of programs that an
    object is reachable or not.
     
    Vincent Cantin, Sep 24, 2004
    #7
  8. Vincent Cantin

    Ann Guest

    "Vincent Cantin" <> wrote in message
    news:...
    > > > If there is no references exept a WeakReference to an object and I

    call
    > > > System.gc(), am I sure that my object has been garbage collected ?

    > >
    > > I'm curious why you care? Why is it important one way or the other?
    > >
    > > If you're using a WeakReference, you've already communicated to the

    system
    > > that your interest in the object is temporal based on the whim of the

    JVM
    > > and the GC, so why are you interested in the specific behavior? How does
    > > this affect your applicaton?
    > >
    > > Regards,
    > >
    > > Will Hartung
    > > ()

    >
    > because I need to know in some points of given class of programs that an
    > object is reachable or not.
    >
    >

    What happens if you compare against null?
     
    Ann, Sep 24, 2004
    #8
  9. Vincent Cantin

    Chris Smith Guest

    Vincent Cantin wrote:
    > because I need to know in some points of given class of programs that an
    > object is reachable or not.


    If you want to know whether the WeakReference is still valid, just use
    get() and compare the result with null. You are not guaranteed that a
    call to System.gc will clear the WeakReference, so you need to check
    yourself if you wish to find out.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 24, 2004
    #9
  10. > > because I need to know in some points of given class of programs that an
    > > object is reachable or not.

    >
    > If you want to know whether the WeakReference is still valid, just use
    > get() and compare the result with null. You are not guaranteed that a
    > call to System.gc will clear the WeakReference, so you need to check
    > yourself if you wish to find out.


    I think nobody understood the problem : What I want to know is wether or not
    my object is reachable.

    To check the value returned by of the WeakReference.get() will not tell me
    what I want to know, since if the answer is not null it doesn't give me the
    information about the reachability of the object.

    I don't a way to know when a weak reference reflect the reachability of the
    object ...
    understood ?
     
    Vincent Cantin, Sep 24, 2004
    #10
  11. > > because I need to know in some points of given class of programs that an
    > > object is reachable or not.

    >
    > If you want to know whether the WeakReference is still valid, just use
    > get() and compare the result with null. You are not guaranteed that a
    > call to System.gc will clear the WeakReference, so you need to check
    > yourself if you wish to find out.


    In fact yes, I will have to find myself .. i.e. I will have to define a
    language interpreted by my java program and handle myself the references.
     
    Vincent Cantin, Sep 24, 2004
    #11
  12. Vincent Cantin

    Chris Uppal Guest

    Vincent Cantin wrote:

    > I think nobody understood the problem : What I want to know is wether or
    > not my object is reachable.


    There is no way that you can do that.

    Not without utilising special knowledge about the behaviour of your program
    that the Java runtime probably doesn't have. (E.g. consider what happens if
    reachablility is affected by different threads.)

    What Java /does/ give you is the ability to find out when the Java runtime has
    /in fact/ found out that an object is no longer reachable by strong references.
    There are various related mechanisms for doing that, aimed at different kinds
    of application.

    Since there are few reasons ever to want to know /immediately/ whether an
    object is reachable (I'm not saying there are none, but I can't think of any
    off-hand) I feel that it is likely that whatever you are trying to do would be
    better achieved in some other way.

    So what /are/ you trying to do ?

    -- chris
     
    Chris Uppal, Sep 24, 2004
    #12
  13. Vincent Cantin wrote:
    >>>because I need to know in some points of given class of programs that an
    >>>object is reachable or not.

    >>
    >>If you want to know whether the WeakReference is still valid, just use
    >>get() and compare the result with null. You are not guaranteed that a
    >>call to System.gc will clear the WeakReference, so you need to check
    >>yourself if you wish to find out.

    >
    >
    > I think nobody understood the problem : What I want to know is wether or not
    > my object is reachable.
    >
    > To check the value returned by of the WeakReference.get() will not tell me
    > what I want to know, since if the answer is not null it doesn't give me the
    > information about the reachability of the object.
    >

    The only sureshot way I can see is to ue a heap profiler.

    http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html

    BK
     
    Babu Kalakrishnan, Sep 24, 2004
    #13
  14. Vincent Cantin

    Adam Guest

    >> > because I need to know in some points of given class of programs
    >> > that an
    >> > object is reachable or not.

    >>
    >> If you want to know whether the WeakReference is still valid, just
    >> use
    >> get() and compare the result with null. You are not guaranteed
    >> that a
    >> call to System.gc will clear the WeakReference, so you need to
    >> check
    >> yourself if you wish to find out.

    >
    > I think nobody understood the problem : What I want to know is
    > wether or not
    > my object is reachable.
    >
    > To check the value returned by of the WeakReference.get() will not
    > tell me
    > what I want to know, since if the answer is not null it doesn't give
    > me the
    > information about the reachability of the object.


    Object yourObject = weakReference.get();
    if(yourObject != null) System.out.println("Yes, your object is
    reachable at this very moment,
    and at least as long as 'yourObject' variable refers it ");

    > I don't a way to know when a weak reference reflect the reachability
    > of the
    > object ...
    > understood ?


    Nope.
     
    Adam, Sep 24, 2004
    #14
  15. Hi,

    > because I need to know in some points of given class of programs that an
    > object is reachable or not.


    Then, of course, the next question should be:

    Why do you want to know at some some in your program that a given class
    is reachable?

    Frankly, I believe there's a conceptional error in a program requiring
    this kind of information to work properly. But without knowing more, it's
    hard to judge.

    So long,
    Thomas
     
    Thomas Richter, Sep 24, 2004
    #15
  16. Vincent Cantin

    Chris Smith Guest

    Vincent Cantin wrote:
    > I think nobody understood the problem : What I want to know is wether or not
    > my object is reachable.


    There is no way to find this out. It's simply not feasible. To
    suddenly determine whether an object is reachable on demand would
    require insane amounts of work. It could take several seconds for some
    applications. The only reason most garbage collection algorithms are at
    all workable is that they defer this work.

    A WeakReference will be cleared as soon as the VM realizes that the
    object isn't reachable through other means. Therefore, you *can* find
    out whether the VM has determined that the object is unreachable or not
    -- and WeakReference is one way to do it. However, it's always possible
    that the VM has not yet realized that the object isn't reachable. There
    is no way in Java to force the VM to do a full garbage collection and
    check that, although System.gc suggests that it try.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 24, 2004
    #16
  17. Vincent Cantin

    xarax Guest

    "Vincent Cantin" <> wrote in message
    news:...
    > > > because I need to know in some points of given class of programs that an
    > > > object is reachable or not.

    > >
    > > If you want to know whether the WeakReference is still valid, just use
    > > get() and compare the result with null. You are not guaranteed that a
    > > call to System.gc will clear the WeakReference, so you need to check
    > > yourself if you wish to find out.

    >
    > In fact yes, I will have to find myself .. i.e. I will have to define a
    > language interpreted by my java program and handle myself the references.


    When the GC places the WeakReference onto the ReferenceQueue,
    then the object is unreachable, but you have to call weakReference.clear()
    when you pull the WeakReference off of the ReferenceQueue. Use
    a separate thread to wait on the ReferenceQueue. Note that using
    get() after the WeakReference has been put onto the ReferenceQueue
    will make the object strongly reachable again, so use some other
    means to identify indirectly the object when you see its WeakReference
    wrapper on the ReferenceQueue.
     
    xarax, Sep 24, 2004
    #17
  18. Vincent Cantin

    Chris Smith Guest

    xarax wrote:
    > Note that using
    > get() after the WeakReference has been put onto the ReferenceQueue
    > will make the object strongly reachable again


    That's not my interpretation of the spec, which says:

    Suppose that the garbage collector determines at a certain point in
    time that an object is weakly reachable. At that time it will
    atomically clear all weak references to that object and all weak
    references to any other weakly-reachable objects from which that
    object is reachable through a chain of strong and soft references.
    At the same time it will declare all of the formerly weakly-
    reachable objects to be finalizable. At the same time or at some
    later time it will enqueue those newly-cleared weak references that
    are registered with reference queues.

    In other words, any WeakReference that's added to a reference queue has
    already been cleared, and a call to get() will return null.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 24, 2004
    #18
  19. Vincent Cantin

    xarax Guest

    "Chris Smith" <> wrote in message
    news:...
    > xarax wrote:
    > > Note that using
    > > get() after the WeakReference has been put onto the ReferenceQueue
    > > will make the object strongly reachable again

    >
    > That's not my interpretation of the spec, which says:
    >
    > Suppose that the garbage collector determines at a certain point in
    > time that an object is weakly reachable. At that time it will
    > atomically clear all weak references to that object and all weak
    > references to any other weakly-reachable objects from which that
    > object is reachable through a chain of strong and soft references.
    > At the same time it will declare all of the formerly weakly-
    > reachable objects to be finalizable. At the same time or at some
    > later time it will enqueue those newly-cleared weak references that
    > are registered with reference queues.
    >
    > In other words, any WeakReference that's added to a reference queue has
    > already been cleared, and a call to get() will return null.


    Oops, you're right. I had some confusion about
    the special behavior of PhantomReferences.
     
    xarax, Sep 24, 2004
    #19
  20. > Object yourObject = weakReference.get();
    > if(yourObject != null) System.out.println("Yes, your object is
    > reachable at this very moment,
    > and at least as long as 'yourObject' variable refers it ");


    No .. this means weakly reachable, and *perhaps* no longer simply reachable.
     
    Vincent Cantin, Sep 25, 2004
    #20
    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. Rob Tillie

    Garbage Collector Debugging

    Rob Tillie, Aug 15, 2003, in forum: ASP .Net
    Replies:
    11
    Views:
    1,766
    JerryK
    Aug 18, 2003
  2. Pyramis
    Replies:
    0
    Views:
    404
    Pyramis
    Jan 25, 2004
  3. Colt

    Garbage collector problem

    Colt, Nov 15, 2003, in forum: Java
    Replies:
    9
    Views:
    690
    Tim Ward
    Nov 18, 2003
  4. bart59
    Replies:
    0
    Views:
    503
    bart59
    Jun 17, 2004
  5. Will Hartung

    Garbage Collector Tuning?

    Will Hartung, Sep 8, 2004, in forum: Java
    Replies:
    3
    Views:
    2,099
    Kevin McMurtrie
    Sep 11, 2004
Loading...

Share This Page