Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > WeakHashMap

Reply
Thread Tools

WeakHashMap

 
 
Abs
Guest
Posts: n/a
 
      06-13-2004
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


--
Abs
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      06-13-2004
Abs wrote:
> 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-13-2004
On Sun, 13 Jun 2004 20:42:08 +0200, Abs <(E-Mail Removed)> wrote or
quoted :

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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Abs
Guest
Posts: n/a
 
      06-13-2004
Roedy Green wrote:
> On Sun, 13 Jun 2004 20:42:08 +0200, Abs <(E-Mail Removed)> wrote or
> quoted :
>
>
>> 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.
>


It seems that the new LinkedHashMap introduced in 1.4 is designed for
this task. Thanks anyway.
 
Reply With Quote
 
Xavier Tarrago
Guest
Posts: n/a
 
      06-14-2004
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.


"Abs" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> 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
>
>
> --
> Abs



 
Reply With Quote
 
sks
Guest
Posts: n/a
 
      06-14-2004

"Xavier Tarrago" <(E-Mail Removed)> wrote in message
news:cajm1j$ndt$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
sks
Guest
Posts: n/a
 
      06-14-2004

"Abs" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Roedy Green wrote:
> > On Sun, 13 Jun 2004 20:42:08 +0200, Abs <(E-Mail Removed)> wrote or
> > quoted :
> >
> >
> >> 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.
> >

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


That's just a map that guarantees the order.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WeakHashMap vs HashMap iksrazal Java 5 12-23-2010 12:20 PM
Re:WeakHashMap rony.john@gmail.com Java 1 01-11-2008 05:07 PM
WeakHashMap gives up to easily Chris Java 5 09-06-2006 11:12 AM
WeakHashMap for values Hendrik Maryns Java 3 06-08-2006 04:40 PM
WeakHashMap question mitch gart Java 2 12-01-2003 10:16 PM



Advertisments