Weak/Soft references?

T

Tegiri Nenashi

And how do you propose to do that? That makes sense in ref-counted
systems, but Java isn't ref-counted...

Legacy limitations... Note however, that ref counted based GCs can be
parallelized, and mark and sweep can't. Is it possible to have a
language where graph of references never has loops (DAG) by design?
 
T

Tegiri Nenashi

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tegiri Nenashi schreef:
|> Well, you’re completely ignoring Joshua’s argument:
|>
|> |> 2 involves less bookkeeping, and so is more robust.
|>
|> Your solution involves a lot of bookkeeping.
|
| Let see. In fancy reference scenario you have a reference that can
| become invalid at any given time. Your application have to check it
| for null and delete it from collection.

No it does not.  It disappears automagically.

Do you imply a weak reference would disappear from collection?

String[] str = new String[1000000];
str[0] = "abc";
WeakReference<String[]> r = new WeakReference<String[]>(str);
LinkedList<WeakReference<String[]>> l = new
LinkedList<WeakReference<String[]>>();
l.add(r);
str = null;
System.gc();
WeakReference<String[]> tmp = l.getFirst(); // NoSuchElementException?
String[] tmp1 = tmp.get();
System.out.println(tmp1[0]);

If LinkedList were purged of null WeakReferences, the it should throw
NoSuchElementException, but it throws NPE a line after that. Sure
collections of WeakReferences are not automatically managed, except
maybe dedicated ones.
 
C

conrad

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Tegiri Nenashi schreef:
|> Well, you’re completely ignoring Joshua’s argument:
|>
|> |> 2 involves less bookkeeping, and so is more robust.
|>
|> Your solution involves a lot of bookkeeping.
|
| Let see. In fancy reference scenario you have a reference that can
| become invalid at any given time. Your application have to check it
| for null and delete it from collection.
No it does not.  It disappears automagically.

Do you imply a weak reference would disappear from collection?

String[] str = new String[1000000];
str[0] = "abc";
WeakReference<String[]> r = new WeakReference<String[]>(str);
LinkedList<WeakReference<String[]>> l = new
LinkedList<WeakReference<String[]>>();
l.add(r);
str = null;
System.gc();
WeakReference<String[]> tmp = l.getFirst(); // NoSuchElementException?
String[] tmp1 = tmp.get();
System.out.println(tmp1[0]);

If LinkedList were purged of null WeakReferences, the it should throw
NoSuchElementException, but it throws NPE a line after that. Sure
collections of WeakReferences are not automatically managed, except
maybe dedicated ones.

That does not match my understanding of WeakReference. It would throw
NoSuchElementException only if the weak reference had been collected,
which it seems in your case did not happen, most likely because you
didn't put any memory pressure on the system so the WeakReference was
not collected.

I'm still a bit weak myself on this matter, so I hope those that
understand WeakReferences better will chime in.
 
D

Daniel Pitts

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Tegiri Nenashi schreef:
|> Well, you’re completely ignoring Joshua’s argument:
|>
|> |> 2 involves less bookkeeping, and so is more robust.
|>
|> Your solution involves a lot of bookkeeping.
|
| Let see. In fancy reference scenario you have a reference that can
| become invalid at any given time. Your application have to check it
| for null and delete it from collection.
No it does not. It disappears automagically.
Do you imply a weak reference would disappear from collection?

String[] str = new String[1000000];
str[0] = "abc";
WeakReference<String[]> r = new WeakReference<String[]>(str);
LinkedList<WeakReference<String[]>> l = new
LinkedList<WeakReference<String[]>>();
l.add(r);
str = null;
System.gc();
WeakReference<String[]> tmp = l.getFirst(); // NoSuchElementException?
String[] tmp1 = tmp.get();
System.out.println(tmp1[0]);

If LinkedList were purged of null WeakReferences, the it should throw
NoSuchElementException, but it throws NPE a line after that. Sure
collections of WeakReferences are not automatically managed, except
maybe dedicated ones.

That does not match my understanding of WeakReference. It would throw
NoSuchElementException only if the weak reference had been collected,
which it seems in your case did not happen, most likely because you
didn't put any memory pressure on the system so the WeakReference was
not collected.

I'm still a bit weak myself on this matter, so I hope those that
understand WeakReferences better will chime in.
Sorry, you're wrong and Tegiri is right.
The WeakReference ITSELF is an Object, and it (weakly) refers to another
object.

The WeakReference object won't get garbage collected unless there are no
references to it. The object that the WeakReference refers too could
get garbage collected at any time, baring some other strong reference to it.

So, if we have Collection->WR->A

'A' might be garbage collected at some time, in which case you would end
up with Collection->WR->(null)

There is ReferenceQueues which will let you know when the Object that a
Reference object refers to is collected.

Hope this helps,
Daniel.

P.S. Look at the implementation of WeakHashMap for more a good lesson.
 
T

Tom Anderson

Legacy limitations... Note however, that ref counted based GCs can be
parallelized, and mark and sweep can't.

I think you might need to go and do some more reading on GC.
Is it possible to have a language where graph of references never has
loops (DAG) by design?

Yes - any language in which it's impossible to mutate existing objects has
this property. That, roughly speaking, is the definition of what are
called 'functional' languages - popular examples would be Haskell, ML,
Erlang, and certain styles of LISP. They're all garbage collected, but i
believe they all use mark-and-sweep rather than refcounting, despite
cycles being impossible.

tom
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top