Ответ 1
Поскольку приложения, использующие NDK, должны вести себя аналогично тем, которые были разработаны с использованием SDK, я думаю, что наилучшее руководство для разумного использования кучи исходит из комментариев ActivityManager.java.
/**
* Return the approximate per-application memory class of the current
* device. This gives you an idea of how hard a memory limit you should
* impose on your application to let the overall system work best. The
* returned value is in megabytes; the baseline Android memory class is
* 16 (which happens to be the Java heap limit of those devices); some
* device with more memory may return 24 or even higher numbers.
*/
public int getMemoryClass() {
return staticGetMemoryClass();
}
/** @hide */
static public int staticGetMemoryClass() {
// Really brain dead right now -- just take this from the configured
// vm heap size, and assume it is in megabytes and thus ends with "m".
String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
}
Код, устанавливающий размер кучи для VM Dalvik, находится в AndroidRuntime.cpp и служит примером того, как определить грубый предел для кучи в собственном коде с помощью функции property_get.
strcpy(heapsizeOptsBuf, "-Xmx");
property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
//LOGI("Heap size: %s", heapsizeOptsBuf);
opt.optionString = heapsizeOptsBuf;
mOptions.add(opt);
Значение по умолчанию 16m
, вероятно, важно, поскольку ни у одного из двух телефонов Android, которые у меня есть, есть свойство dalvik.vm.heapsize
, установленное по умолчанию.