WeakHashMap

Discussion in 'Java' started by Abs, Jun 13, 2004.

  1. Abs

    Abs Guest

    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
    Abs, Jun 13, 2004
    #1
    1. Advertising

  2. Abs

    Chris Smith Guest

    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
    Chris Smith, Jun 13, 2004
    #2
    1. Advertising

  3. Abs

    Roedy Green Guest

    On Sun, 13 Jun 2004 20:42:08 +0200, Abs <> 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.
    Roedy Green, Jun 13, 2004
    #3
  4. Abs

    Abs Guest

    Roedy Green wrote:
    > On Sun, 13 Jun 2004 20:42:08 +0200, Abs <> 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.
    Abs, Jun 13, 2004
    #4
  5. 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" <> a écrit dans le message de
    news:...
    > 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
    Xavier Tarrago, Jun 14, 2004
    #5
  6. Abs

    sks Guest

    "Xavier Tarrago" <> wrote in message
    news:cajm1j$ndt$...
    > 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.
    sks, Jun 14, 2004
    #6
  7. Abs

    sks Guest

    "Abs" <> wrote in message news:...
    > Roedy Green wrote:
    > > On Sun, 13 Jun 2004 20:42:08 +0200, Abs <> 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.
    sks, Jun 14, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mitch gart

    WeakHashMap question

    mitch gart, Nov 17, 2003, in forum: Java
    Replies:
    2
    Views:
    1,120
    Adam Jenkins
    Dec 1, 2003
  2. iksrazal

    WeakHashMap vs HashMap

    iksrazal, Feb 10, 2004, in forum: Java
    Replies:
    5
    Views:
    22,535
    dearmayank
    Dec 23, 2010
  3. Hendrik Maryns

    WeakHashMap for values

    Hendrik Maryns, Jun 8, 2006, in forum: Java
    Replies:
    3
    Views:
    1,266
    Hendrik Maryns
    Jun 8, 2006
  4. Chris
    Replies:
    5
    Views:
    740
  5. Re:WeakHashMap

    , Jan 11, 2008, in forum: Java
    Replies:
    1
    Views:
    367
    Lasse Reichstein Nielsen
    Jan 11, 2008
Loading...

Share This Page