11

The size of the VM heap cannot exceed 16mb, 24mb, 32mb depending on the phone. But what is the maximum size of the native heap? How much native memory can be allocated to the app when it is in foreground.

Thanks.

dcanh121
  • 4,665
  • 11
  • 37
  • 84

3 Answers3

13

Technically there's no restriction in the NDK. Someone asked this a while back and was referred to this android-ndk Groups thread. A relevent quote:

"Also given that this is the NDK list, the limit is actually not imposed on you, because it is only on the Java heap. There is no limit on allocations in the native heap..."

Dianne Hackborn

She does go on to say that it shouldn't be abused and if it is than applications could be killed.

Community
  • 1
  • 1
Pedantic
  • 5,032
  • 2
  • 24
  • 37
  • On Android O, things seem to have changed in terms of memory usage: I think Bitmaps are stored on the native memory. How would you deal with it? For example, how would you decide of mem-cache size? I've asked about this here: https://stackoverflow.com/q/48091403/878126 – android developer Jan 04 '18 at 09:09
2

adb shell dumpsys meminfo PACKAGENAME will give you native and dalvik memory usage of your app.

jpelletier
  • 133
  • 1
  • 8
2

There's no simple answer to this; you can use as much memory as the device has, minus what it's using for other programs. When Android thinks it's low on memory, it'll start killing background tasks, so it's a soft limit. Most devices do not have swap space. You can get some statistics about the device's memory from inside Dalvik with android.app.ActivityManager.MemoryInfo (I assume there's an NDK equivalent).

jimrandomh
  • 895
  • 8
  • 15