WeakHashMap

A

Abs

Hi!

I'd like to do image caching for the thumbnail browser app I'm
developing. Some people have told me to use the WeakHashMap for it, but
other people have recommended me against using it because it doesn't act
as a true cache. What can I use to develop an image chache for my app ?


Thanks in advance
 
C

Chris Smith

Abs said:
I'd like to do image caching for the thumbnail browser app I'm
developing. Some people have told me to use the WeakHashMap for it, but
other people have recommended me against using it because it doesn't act
as a true cache. What can I use to develop an image chache for my app ?

Depends on what you want.

WeakHashMap will make absolutely no effort at all to preserve the
contents of the map. At any convenient opportunity, it will happily
throw away all of your cached thumbnails, even if there is plenty of
memory for them. This would be appropriate if your goal is only to
avoid reloading thumbnails that are already being actively used
elsewhere in your code.

Alternatively, you could put together something like WeakHashMap, but
using SoftReference instead of WeakReference instead. That would only
release memory when it's needed.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

I'd like to do image caching for the thumbnail browser app I'm
developing. Some people have told me to use the WeakHashMap for it, but
other people have recommended me against using it because it doesn't act
as a true cache. What can I use to develop an image chache for my app ?

For a "true cache" you would use HashMap, which holds onto everything,
but that is not a true cache in my opinion. See
http://mindprod.com/jgloss/caching.html.


A cache usually holds onto only SOME of the material. The advantage of
WeakHashMap is automatically holds onto more when you have sufficient
RAM, and jettisons when you don't.
 
A

Abs

Roedy said:
For a "true cache" you would use HashMap, which holds onto everything,
but that is not a true cache in my opinion. See
http://mindprod.com/jgloss/caching.html.


A cache usually holds onto only SOME of the material. The advantage of
WeakHashMap is automatically holds onto more when you have sufficient
RAM, and jettisons when you don't.

It seems that the new LinkedHashMap introduced in 1.4 is designed for
this task. Thanks anyway.
 
X

Xavier Tarrago

WeakHashMap is not well suited for cache but rather for canonical mapping.
It maintains alive a reference to a resource associated with a key as long
as the key is alive.

I had this need (caching) but were not able to find some help. I tried
SoftReference, but found that it is flawed. I got OutOfMemory exception if I
build soft referenced objects too fast. I looked around about that, and
found on the sun java site that this behaviour was correct (I donot agree).

At last, I created my own cache with an array of WeakReference and a
circular buffer of strong references to retain lru reference in memory.

I am interrested with any pointers about that topic.
 
S

sks

Xavier Tarrago said:
WeakHashMap is not well suited for cache but rather for canonical mapping.
It maintains alive a reference to a resource associated with a key as long
as the key is alive.

I had this need (caching) but were not able to find some help. I tried
SoftReference, but found that it is flawed. I got OutOfMemory exception if I
build soft referenced objects too fast. I looked around about that, and
found on the sun java site that this behaviour was correct (I donot agree).

At last, I created my own cache with an array of WeakReference and a
circular buffer of strong references to retain lru reference in memory.

What you want really is a SoftHashMap should one exist. I think jakarta
commons has one. Basically a HashMap of keys to soft referenced values.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top