Android NDK cannot allocate much memory

I have an NDK application that needs to allocate a large amount of memory (50MB), but unfortunately malloc () returns null and the errno value is 12, indicating ENOMEM.

However, just before I launch the application, I see that "Device Memory" shows that I have 1.7 GB of free memory. In addition, heapsize / heapmaxfree should not be displayed when allocating memory from application "C". I have added this information below.

[dalvik.vm.heapgrowthlimit]: [128m]
[dalvik.vm.heapmaxfree]: [8m]
[dalvik.vm.heapminfree]: [2m]
[dalvik.vm.heapsize]: [512m]
[dalvik.vm.heapstartsize]: [8m]
[dalvik.vm.heaptargetutilization]: [0.75]

      

What could be the reason why I cannot allocate a large amount of memory using malloc () from my NDK code?

Also I used ActivityManager API to retrieve memory class and avaialble memory and it shows the following output and my manifest request includes android: largeHeap = "true"

onCreate GetMemoryClass => 128
onCreate getLargeMemoryClass => 512
onCreate avmem => 456 MB

MemoryInfo mi = new MemoryInfo();
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.getMemoryInfo(mi);
long avmem = mi.availMem / 1048576L;
Log.d(mTAG, "onCreate GetMemoryClass => " + am.getMemoryClass() );
Log.d(mTAG, "onCreate getLargeMemoryClass => " + am.getLargeMemoryClass() );
Log.d(mTAG, "onCreate avmem => " + avmem + " MB");

      

So it keeps puzzling me why there is 450MB of memory and malloc () cannot allocate 50MB of memory, is it something broke on the memory management part or should it be?

I am running my program on Android 4.4.1 on a Galaxy S4 device.

+3


source to share





All Articles