由于使用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));
}
,用于设置堆大小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
属性默认设置的可能非常重要。
谢谢蒂姆。这有点吓人,保持在16MB以下。如果我理解这个权利,那么可以通过手机制造商构建的操作系统来实现上限,以便每个应用程序只能访问一定数量的内存。如果没有设置上限,那么每个应用程序可以根据需要从可用RAM中分配尽可能多的内容(当然,因为它需要)?你是说当getMemoryClass()在手机上运行时,它们返回null?你有哪些手机,如果我可能如此大胆地问...有没有可能有关于新手机限制信息的地方? (我完全脱离基地吗?) – Maximus 2010-06-16 14:32:37
我真的应该首先在Dianne Hackborn搜索NDK团队...... “另外,鉴于这是NDK列表,实际上并没有强加给你,因为它只在Java堆上。本地堆中的分配没有限制......” 她确实继续说它不应该被滥用,如果是,应用程序可能会被杀死。这是主题,一般来说,这是一个很好的主题阅读。 http://groups.google.com/group/android-ndk/browse_thread/thread/95c9f0cecceb112a/ab033b9a28d52fcd?lnk=gst&q=heap+limit#ab033b9a28d52fcd – Maximus 2010-06-16 15:03:33
@Maximus戴安娜的意见概括起来非常好。尽管NDK可以分配多少堆没有限制(运行'ulimit -d'返回'unlimited'),但使用所有可用内存会带来一些后果。 – 2010-06-17 04:12:14