Hi there,

I'm using a caching mechanism for some objects which have a small
memory footprint but are computation expensive to instantiate. Also,
I'm using a loop to prebuffer all those objects on application
startup, so that they are already in my cache when my UI starts
displaying them later.

Right now, my objects are so few and so small that I might just keep
them all in memory. I first coded my caching based on standard
references and it worked just fine. But my objectsd might grow in size
and/or number, so I started using java.lang.ref.SoftReference inside
my cache so that they won't cause an OutOfMemoryException.

I have to admit that I never used soft references before, but after
reading I'm rather sure I understood them correctly.

Actually, something like that happens:
Object 1 is cached
Object 2 is cached
Object 3 is cached
Object 4 is cached
Object 5 is cached
Objects 1 to 4 are cleared by the GC
Object 6 is cached
Object 7 is cached
Object 8 is cached
Object 9 is cached
Objects 5 to 8 are cleared by the GC
and so on.

So each time the UI want to display e.g. Object 1, it's not in the
cache and I must recostruct it which takes about 300ms. Because of
this, my caching is completely pointless.

I know that this is legal behavior for Soft References: The GC may or
may not clear them when he encounters them, ideally dependant on the
amount of free memory, and it must clear all of them before an out of
memory exception is thrown.

The only odd thing is, my memory is mostly free, the heap grows up to
3 MB but the GC keeps the real usage under 2 MB most of the time.
After some time running, when each object has been reconstructed 2 or
3 times (and the user has been annoyed by the time it took), suddently
the GC changes its mind and stops clearing the refenreces, and all
objects (about 40) are kept in memory and the UI works smoothly and my
caching makes sense.

Is there any way to bias the way those references are handled, e.g.
tell the GC that it is totally ok to have a heap of 4 MB? In the end,
thats what the GC does automatically after 30 seconds of doint it
wrong.

with best regards,
Brian Schimmel

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to