Name: tb29552 Date: 02/22/99
I found that it is very complicated to write a generic
object cache. I used Proxy Objects (holding the
"primary key" data, e.g. a file name used to reload
e.g. an image) with Weak/SoftReferences to the large
data (e.g. the image/blob). SoftReferences didn't
really work because they were too fast reclaimed
by the virtual machine and I was unable to specify
the order (which object should be reclaimed first).
So I found a solution, where I still use the proxys,
but use a dynamically sized list that holds hard references
to the most used images/blobs. The garbage collector now
may delete all weak referenced images in the proxys, but
not the e.g. 100 most used ones, because the list holds
hard references to them.
My intention was to allow the list to dynamically grow
(lots of memory free) or shrink (memory needed by other
parts of the application). That lead me into big trouble.
The only way I know is freeMemory()/totalMemory(). I set up
a thread that read these values and resized the cache list.
That lead me into OutOfMemoryErrors, because I found out that
there were peaks between the points my thread asked for the
values. So I tried catching OutOfMemoryError, so that the
catch handler shrinks my list. That doesn't work either,
because I ran into problems when there were other lines
in my code that needed only a few bytes, but failed. But I
could not forsee the memory peaks (I reserved >3 MB, but that
was not sufficient for the peaks!).
-> Currently I see no efficient way, to communicate with the garbage
I'd like to aks the garbage collector, if he recommends
me to be careful with memory or if he says "well, 2 MB, no problem,
lots of memory there for you, maybe I'll collect a little bit".
I also like to register some class in my program as a
"LowMemoryListener" that has the chance of freeing memory
_before_ the garbage collector issues a OutOfMemoryError.
And I would like to get some statistical information from
the garbage collector, because there is no way to get the
information in my program that a human can see when using
Maybe there could also be a Listener that is informed whenever
the garbage collector ran and grab that information or recalc
the size of self made caches.
(Review ID: 54528)