Using SoftReferences for caching

Discussion in 'Java' started by Jesper Nordenberg, Feb 12, 2004.

  1. I'm writing an application where I have a memory cache of objects
    stored on disk. There are no limitations on the object sizes, one
    object can be as large as the heap. A long value is used to identify
    an object, so there can be a huge amount of objects.

    I want to use a fixed amount of the heap or all the available heap for
    the cache. I can't cache a fixed number of objects since I don't know
    how how much memory an object occupies, and thus the program can run
    out of memory. So, using SoftReferences seems like a good solution.
    One problem is that the HashMap used in the cache can become very big
    and needs to be cleared of SoftReferences that has been nullified.

    If someone have some implementation or design suggestion that would be
    great. Maybe there is some open source project that implement a
    similar cache?

    /Jesper Nordenberg
    Jesper Nordenberg, Feb 12, 2004
    #1
    1. Advertising

  2. Jesper Nordenberg

    Conor White Guest

    You need to associate each SoftReference with a ReferenceQueue object.
    When a SoftReference is Garbage collected it is placed in the
    ReferenceQueue. This gives you a chance to do a cleanup before the
    object is finally lost.


    (Jesper Nordenberg) wrote in message news:<>...
    > I'm writing an application where I have a memory cache of objects
    > stored on disk. There are no limitations on the object sizes, one
    > object can be as large as the heap. A long value is used to identify
    > an object, so there can be a huge amount of objects.
    >
    > I want to use a fixed amount of the heap or all the available heap for
    > the cache. I can't cache a fixed number of objects since I don't know
    > how how much memory an object occupies, and thus the program can run
    > out of memory. So, using SoftReferences seems like a good solution.
    > One problem is that the HashMap used in the cache can become very big
    > and needs to be cleared of SoftReferences that has been nullified.
    >
    > If someone have some implementation or design suggestion that would be
    > great. Maybe there is some open source project that implement a
    > similar cache?
    >
    > /Jesper Nordenberg
    Conor White, Feb 12, 2004
    #2
    1. Advertising

  3. Jesper Nordenberg

    Carl Howells Guest

    Conor White wrote:
    > You need to associate each SoftReference with a ReferenceQueue object.
    > When a SoftReference is Garbage collected it is placed in the
    > ReferenceQueue. This gives you a chance to do a cleanup before the
    > object is finally lost.
    >


    Be careful which object you're talking about... When a SoftReference is
    placed in a ReferenceQueue, the object the SoftReference referred to has
    already been collected. There is no way to get the object back at that
    point. All you can get out of the ReferenceQueue is the SoftReference.

    This does still solve the OP's problem... But be careful how you phrase
    things.
    Carl Howells, Feb 12, 2004
    #3
  4. (Conor White) wrote in message news:<>...
    > You need to associate each SoftReference with a ReferenceQueue object.
    > When a SoftReference is Garbage collected it is placed in the
    > ReferenceQueue. This gives you a chance to do a cleanup before the
    > object is finally lost.


    I tried this. Two problems I encountered:

    - You need another (reverse) HashMap to look up the entry of the
    SoftReference that was placed in the ReferenceQueue.

    - When testing with multiple threads I sometimes ran out of memory. It
    seemed there was some nullified SoftReferences in the map that haven't
    been placed in the ReferenceQueue. Maybe it was an error in my
    program.

    My current solution is to regurarly traverse the map and remove
    nullified SoftReferences.

    /Jesper Nordenberg
    Jesper Nordenberg, Feb 13, 2004
    #4
  5. Jesper Nordenberg

    Adam Jenkins Guest

    Jesper Nordenberg wrote:
    > (Conor White) wrote in message news:<>...
    >
    >>You need to associate each SoftReference with a ReferenceQueue object.
    >>When a SoftReference is Garbage collected it is placed in the
    >>ReferenceQueue. This gives you a chance to do a cleanup before the
    >>object is finally lost.

    >
    >
    > I tried this. Two problems I encountered:
    >
    > - You need another (reverse) HashMap to look up the entry of the
    > SoftReference that was placed in the ReferenceQueue.


    Typically you would just use your own subclass of SoftReference, with
    some extra fields containing whatever information you need to clean up
    after it. No need for a reverse map.

    Adam
    Adam Jenkins, Feb 13, 2004
    #5
  6. Jesper Nordenberg

    Conor White Guest

    Look at the code for the java.util.WeakHashMap class. This does what
    you want except it uses weak references instead of soft references.

    Adam Jenkins <> wrote in message news:<>...
    > Jesper Nordenberg wrote:
    > > (Conor White) wrote in message news:<>...
    > >
    > >>You need to associate each SoftReference with a ReferenceQueue object.
    > >>When a SoftReference is Garbage collected it is placed in the
    > >>ReferenceQueue. This gives you a chance to do a cleanup before the
    > >>object is finally lost.

    > >
    > >
    > > I tried this. Two problems I encountered:
    > >
    > > - You need another (reverse) HashMap to look up the entry of the
    > > SoftReference that was placed in the ReferenceQueue.

    >
    > Typically you would just use your own subclass of SoftReference, with
    > some extra fields containing whatever information you need to clean up
    > after it. No need for a reverse map.
    >
    > Adam
    Conor White, Feb 15, 2004
    #6
    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. Hypo
    Replies:
    6
    Views:
    406
  2. Troy Simpson

    Fragment Caching inside page caching?

    Troy Simpson, Jan 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    783
    Troy Simpson
    Jan 19, 2004
  3. Joe
    Replies:
    11
    Views:
    835
    steve
    Apr 2, 2006
  4. Kevin McMurtrie

    Re: Controlling SoftReferences

    Kevin McMurtrie, Jul 4, 2008, in forum: Java
    Replies:
    3
    Views:
    292
    John B. Matthews
    Jul 7, 2008
  5. JimLad
    Replies:
    3
    Views:
    911
    JimLad
    Jan 21, 2010
Loading...

Share This Page