Can I convert Weak references to "hard" references ?

L

Lars Willich

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
 
B

Bent C Dalager

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
 
L

Lew

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.
 
H

Hunter Gratzner

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.
 
L

Lew

Hunter said:
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.
 
M

Mark Space

Lars said:
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...
 
D

Daniel Pitts

Mark said:
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.
 
H

Hendrik Maryns

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-----
 
C

Christopher Benson-Manica

[comp.lang.java.programmer] Hendrik Maryns said:
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.
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.
 
B

Bent C Dalager

-=-=-=-=-=-

Lars Willich schreef:


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
 
I

Ingo R. Homann

Hi,

concerning the API-doku:

Lars said:
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
 
I

Ian Shef

(e-mail address removed) (Lars Willich) wrote in

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?
 

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top