Can I convert Weak references to "hard" references ?

Discussion in 'Java' started by Lars Willich, Oct 21, 2007.

  1. Lars Willich

    Lars Willich Guest

    I have problems because of Java crashes after garbage collection of weak referenced objects.

    Can I somehow:

    a) Convert weak references into hard references ?
    b) at least get a notication when a weak referenced object is garbage collected ?

    Lars
     
    Lars Willich, Oct 21, 2007
    #1
    1. Advertising

  2. Lars Willich

    Roedy Green Guest

    On 21 Oct 2007 11:15:28 GMT, (Lars Willich) wrote,
    quoted or indirectly quoted someone who said :

    >a) Convert weak references into hard references ?


    you would just add a hard reference to the object.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 21, 2007
    #2
    1. Advertising

  3. In article <471b34cf$0$27140$-online.net>,
    Lars Willich <> wrote:
    >I have problems because of Java crashes after garbage collection of weak
    >referenced objects.
    >
    >Can I somehow:
    >
    >a) Convert weak references into hard references ?


    WeakReference weak = ...;

    ....

    Object strong = weak.get();
    if (strong != null)
    {
    // store the strong reference somewhere if you don't want
    // it to be garbage collected.
    }
    else
    {
    // You're out of luck - the weakref has already been
    // collected
    }

    >b) at least get a notication when a weak referenced object is garbage
    >collected ?


    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ref/ReferenceQueue.html
    might do what you want.

    Cheers,
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Oct 21, 2007
    #3
  4. Lars Willich

    Lew Guest

    Lars Willich wrote:
    >> I have problems because of Java crashes after garbage collection of weak
    >> referenced objects.


    What is the nature of these crashes? Is it because you aren't guarding
    against the consequences of reaping WeakReferences after they've been garbage
    collected?

    Crashes like that don't "just happen"; there's a reason and it's in your code.

    --
    Lew
     
    Lew, Oct 21, 2007
    #4
  5. On Oct 21, 3:42 pm, Lew <> wrote:
    > Lars Willich wrote:
    > Crashes like that don't "just happen"; there's a reason and it's in your code.
    >


    There is also a chance it is in Sun's code, e.g. in the Java or native
    code of the VM. VM crashes are rare but do happen.

    To the OP: Post the complete original error message. Don't paraphrase,
    use copy-paste. Don't forget to tell us the Java version and your OS,
    too.
     
    Hunter Gratzner, Oct 21, 2007
    #5
  6. Lars Willich

    Lew Guest

    Lew wrote:
    >> Crashes like that don't "just happen"; there's a reason and it's in your code.


    Hunter Gratzner wrote:
    > There is also a chance it is in Sun's code, e.g. in the Java or native
    > code of the VM. VM crashes are rare but do happen.


    I assumed the OP at least was accurate to the extent of the description given.
    If their error message had nothing to do with weak references, of course,
    you're right. If the crash is directly related to the use of weak references,
    then I stand by my assessment. I should have done what you did, ask for the
    actual facts before rendering judgment.

    FWIW, there are no active weak-reference-related VM-crashing bugs listed at Sun.

    > To the OP: Post the complete original error message. Don't paraphrase,
    > use copy-paste. Don't forget to tell us the Java version and your OS,
    > too.


    --
    Lew
     
    Lew, Oct 21, 2007
    #6
  7. Lars Willich

    Lew Guest

    Lars Willich wrote:
    >> I have problems because of Java crashes after garbage collection of weak
    >> referenced objects.

    ....
    >> b) at least get a notication when a weak referenced object is garbage
    >> collected ?


    Bent C Dalager wrote:
    > Object strong = weak.get();
    > if (strong != null)
    > {
    > // store the strong reference somewhere if you don't want
    > // it to be garbage collected.
    > }
    > else
    > {
    > // You're out of luck - the weakref has already been
    > // collected
    > }
    >
    >
    > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ref/ReferenceQueue.html
    > might do what you want.


    There, he's given you two different approaches.

    --
    Lew
     
    Lew, Oct 21, 2007
    #7
  8. Lars Willich

    Mark Space Guest

    Lars Willich wrote:

    > a) Convert weak references into hard references ?


    I haven't actually tried this, but I think it would work

    Given some WeakReference wf:

    Object hardRef = wf;
    if( hardRef == null )
    // Reload the resource
    ;
    ZZZ zzz = (ZZZ) hardRef; // Need to cast to use it...
     
    Mark Space, Oct 21, 2007
    #8
  9. Lars Willich

    Daniel Pitts Guest

    Mark Space wrote:
    > Lars Willich wrote:
    >
    >> a) Convert weak references into hard references ?

    >
    > I haven't actually tried this, but I think it would work
    >
    > Given some WeakReference wf:
    >
    > Object hardRef = wf;
    > if( hardRef == null )
    > // Reload the resource
    > ;
    > ZZZ zzz = (ZZZ) hardRef; // Need to cast to use it...
    >

    That doesn't work.

    Although, I think the OP incorrectly described his problem. If he has a
    problem with WeakReference, its most likely his own use of it that is
    wrong. He needs to show us the exact error.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 21, 2007
    #9
  10. Lars Willich schreef:
    > I have problems because of Java crashes after garbage collection of weak referenced objects.
    >
    > Can I somehow:
    >
    > a) Convert weak references into hard references ?


    By subclassing WeakReference and including a real reference to the
    object in it. Depends on what you want to do, of course.

    > b) at least get a notication when a weak referenced object is garbage collected ?


    Write a finaliser for your subclass.

    H.
    --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD8DBQFHHIM5e+7xMGD3itQRAjQhAJwOlyr+ZiFBMRtY3uAPB+TXT5MvDwCbBviY
    KbGTRcDLuImEL9ymxK5y06Q=
    =eZZC
    -----END PGP SIGNATURE-----
     
    Hendrik Maryns, Oct 22, 2007
    #10
  11. [comp.lang.java.programmer] Hendrik Maryns <> wrote:

    > Lars Willich schreef:


    >> a) Convert weak references into hard references ?


    > By subclassing WeakReference and including a real reference to the
    > object in it. Depends on what you want to do, of course.


    Making a WeakReference actually behave as a "strong" reference strikes
    me as a bad plan.

    >> b) at least get a notication when a weak referenced object is garbage collected ?


    > Write a finaliser for your subclass.


    Won't work. The referent won't be garbage collected unless the real
    reference is cleared, and then you're back to a WeakReference.

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
     
    Christopher Benson-Manica, Oct 22, 2007
    #11
  12. In article <ffhvvh$2fk$-tuebingen.de>,
    Hendrik Maryns <> wrote:
    >-=-=-=-=-=-
    >
    >Lars Willich schreef:
    >
    >> b) at least get a notication when a weak referenced object is garbage collected ?

    >
    >Write a finaliser for your subclass.


    This isn't really recommended as it may (I have a feeling that it
    /will/) cause the garbage collector to handle those objects in a
    completely different manner than it otherwise would have, and it is
    likely to degrade the performance of the collector.

    Cheers,
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Oct 22, 2007
    #12
  13. Hi,

    concerning the API-doku:

    Lars Willich wrote:
    > I have problems because of Java crashes after garbage collection of weak referenced objects.
    >
    > Can I somehow:
    >
    > a) Convert weak references into hard references ?


    Yes:

    T hardRef=weakRef.get();

    > b) at least get a notication when a weak referenced object is garbage collected ?


    Yes:

    IIRC, the class ReferenceQueue is designed for that case.

    Overriding the finalize-method should work as well. (Note that
    finalizers are often declared as buggy, deprecated and unsafe, but work
    perfectly in practise.)

    Ciao,
    Ingo
     
    Ingo R. Homann, Oct 23, 2007
    #13
  14. Lars Willich

    Ian Shef Guest

    (Lars Willich) wrote in
    news:471b34cf$0$27140$-online.net:

    > I have problems because of Java crashes after garbage collection of weak
    > referenced objects.
    >
    > Can I somehow:
    >
    > a) Convert weak references into hard references ?
    > b) at least get a notication when a weak referenced object is garbage
    > collected ?
    >
    > Lars
    >
    >


    Doesn't get() (inherited from java.lang.ref.Reference) provide these?
    get() returns the hard reference, or returns null if the referenced object
    has been cleared (not necessarily garbage collected yet).

    Are you sure that this is really the problem, and are you sure that this is
    the right solution to your problem? If you have weak referenced objects,
    then isn't there an expectation that they will be garbage collected? If you
    are going to turn weak references into hard references, why have the weak
    references in the first place?



    --
    Ian Shef 805/F6 * These are my personal opinions
    Raytheon Company * and not those of my employer.
    PO Box 11337 *
    Tucson, AZ 85734-1337 *
     
    Ian Shef, Oct 23, 2007
    #14
    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. Kuan Zhou
    Replies:
    1
    Views:
    5,017
    Paul Uiterlinden
    Jan 24, 2005
  2. Nicholas Zigarovich

    Strange behavior with weak references

    Nicholas Zigarovich, Sep 2, 2003, in forum: Java
    Replies:
    0
    Views:
    372
    Nicholas Zigarovich
    Sep 2, 2003
  3. Mark M
    Replies:
    16
    Views:
    725
    Chris Uppal
    Mar 5, 2004
  4. Tegiri Nenashi

    Weak/Soft references?

    Tegiri Nenashi, Jul 9, 2008, in forum: Java
    Replies:
    64
    Views:
    1,802
    Tom Anderson
    Jul 22, 2008
  5. Caleb Clausen
    Replies:
    7
    Views:
    164
    Logan Capaldo
    Feb 6, 2006
Loading...

Share This Page