Low memory warnings

235 views
Skip to first unread message

Phil Endecott

unread,
Nov 25, 2010, 12:29:29 PM11/25/10
to android-ndk
Dear Experts,

Does anyone have any advice about how native code should react to the
onLowMemory() method of the Java activity class?

My app has some large caches which can be flushed when necessary. The
question is how much of their content should I discard, and should I
perhaps be discarding stuff before I even get this low memory
indication. I've done some experiments on a device with 512 MB of
RAM, and I get a first warning when my cache size is about 250 MB. If
I discard 20 MB on each warning, the app will eventually be killed; if
I discard 100 MB, it seems to keep going indefinitely. Discarding too
much is not great because it makes the app stutter as the content is
reloaded, but that's better than crashing.

I have the same problem with the iPhone version of the app, where I've
determined a formula for how much to discard based on the amount of
physical RAM. This feels fragile but at least there's a fairly small
number of devices that I have to test it on. It would be so much
easier if the OS would just tell me how much space it needs in the
warning.

Has anyone else tried to deal with this? Does anyone know in detail
the conditions under which this warning is sent?


Thanks, Phil.

Dianne Hackborn

unread,
Nov 25, 2010, 2:05:16 PM11/25/10
to andro...@googlegroups.com
This callback is when memory is becoming critical.  You should free up as much as you can.

Btw 250MB is way too large for just "cache" memory.  Look at ActivityManager.getMemoryClass() to get a hint of how large you should keep your heap (this is the limit *imposed* on the Java heap, but native apps don't have this limit though more and more it is looking like stronger control over them is needed); you will see you are getting into an order of magnitude out from it.

By the time this callback has happened, you have already impacted device performance enough to be something the user is aware of -- *all* background processes are gone, and the only thing left to get rid of are things that truly want to be running.  As a native developer, it is your responsibility to keep your allocations under control so this doesn't generally happen.


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




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Phil Endecott

unread,
Nov 25, 2010, 6:49:48 PM11/25/10
to android-ndk
On Nov 25, 7:05 pm, Dianne Hackborn <hack...@android.com> wrote:
> This callback is when memory is becoming critical. You should free up as
> much as you can.

And there is no earlier warning, right?

> Btw 250MB is way too large for just "cache" memory. Look at
> ActivityManager.getMemoryClass() to get a hint of how large you should keep
> your heap (this is the limit *imposed* on the Java heap, but native apps
> don't have this limit though more and more it is looking like stronger
> control over them is needed); you will see you are getting into an order of
> magnitude out from it.

So you're suggesting that on a device with half a gigabyte of RAM, I'm
only supposed to use 30 megabytes?! Buy shares in RAM manufacturers!

Come on. What is all that half gigabyte being used for? If I limit
my cache to 30 MBytes, the user constantly sees stuff being swapped in
and out. If I let it use 200 MBytes the device functions fine. How
do I find the real limit? Shall I just get the available physical RAM
and use half of it, or something?


Thanks, Phil.
Reply all
Reply to author
Forward
0 new messages