由於使用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