下面是我的公式,用于检查剩余的内存量(不是当前堆中剩余的内存量,而是在应用程序崩溃之前可以利用多少内存)。我不确定这是正确的,是吗?

double max = Runtime.getRuntime().maxMemory(); //the maximum memory the app can use
double heapSize = Runtime.getRuntime().totalMemory(); //current heap size
double heapRemaining = Runtime.getRuntime().freeMemory(); //amount available in heap
double nativeUsage = Debug.getNativeHeapAllocatedSize(); //is this right? I only want to account for native memory that my app is being "charged" for.  Is this the proper way to account for that?

//heapSize - heapRemaining = heapUsed + nativeUsage = totalUsage
double remaining = max - (heapSize - heapRemaininng + nativeUsage);

最佳答案

试试下面的代码。这应该给您带来您想要的结果(尤其是“Pss”字段)。您可以阅读有关它的更多信息here

Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(memoryInfo);

String memMessage = String.format(
    "Memory: Pss=%.2f MB, Private=%.2f MB, Shared=%.2f MB",
    memoryInfo.getTotalPss() / 1024.0,
    memoryInfo.getTotalPrivateDirty() / 1024.0,
    memoryInfo.getTotalSharedDirty() / 1024.0);

08-18 04:36