I have been a java developer 2 years. But I have never wrote a WeakReference in my code. How to use WeakReference to make my application more efficiency especially in Android application.

share|improve this question
5  
Please notice that sometimes you should mark the answers as correct. Otherwise, your accept rate remains very low (currently is at 7%!) and the other users might start feeling like not answering your questions. – Pedro Morte Rolo Mar 14 '12 at 16:09
1  
Just accept the answer already... – Alex Lockwood May 13 '12 at 19:00
Do people really concern themselves so much with being accepted that they stop getting involved at all? That's really sad... – TwistedUmbrella Aug 8 '12 at 15:26
3  
@Chris - DByrne's reply was a great answer two years ago. It's still a great answer today. "Accept" it already, will you???? The karma will do you good ;) – paulsm4 Aug 15 '12 at 15:44

1 Answer

Using a WeakReference in Android isn't any different than using one in plain old Java. Here is a great guide which gives a detailed explanation: Understanding Weak References.

You should think about using one whenever you need a reference to an object, but you don't want that reference to protect the object from the garbage collector. A classic example is a cache that you want to be garbage collected when memory usage gets too high (often implemented with WeakHashMap).

Be sure to check out SoftReference and PhantomReference as well.

EDIT: Tom has raised some concerns over implementing a cache with WeakHashMap. Here is an article laying out the problems: WeakHashMap is not a cache!

Tom is right that there have been complaints about poor Netbeans performance due to WeakHashMap caching.

I still think it would be a good learning experience to implement a cache with WeakHashMap and then compare it against your own hand-rolled cache implemented with SoftReference. In the real world, you probably wouldn't use either of these solutions, since it makes more sense to use a 3rd party library like Apache JCS.

share|improve this answer
+1 for WeakHashMap and cache reference – Tim Bender Jul 14 '10 at 4:46
3  
No! No! No! WeakHashMap used as a cache is fatal. Entries can be removed as soon as they are created. This probably will not happen when you are testing, but may well when in use. Of note, NetBeans can be brought to an effective 100% CPU stop by this. – Tom Hawtin - tackline Jul 14 '10 at 9:22
1  
@Tom I've updated my answer. To be fair though, I was technically correct that caches ARE often implemented with WeakHashMap even if you are correct that it is a bad choice ;) – dbyrne Jul 14 '10 at 12:53
1  
Excellent answer by dbyrne. Thanks for this. I see no reason for the chris not to accept this answer. – san Feb 22 '12 at 8:04
@dbyrne I'm using objects in my Activity like GridView, ImageView or BaseAdapter. In the onDestroy method, when I finish the activity, do I need to do something with this objects using Weak/SoftReferences? Or the system clean automatically this memory of this object? – beni Feb 20 at 15:37
show 1 more comment

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.