2017-08-02 61 views
3

我想要我的應用程序堆內存大小檢查。如何在android上檢查我的應用程序堆內存大小?

拳頭我用android:largeHeap="true"

如何在Android上我的應用程序的堆內存大小檢查?

+0

可能重複的問題。看到這裏https://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android – Raheel

+0

除非你有一個非常獨特的應用程序,你不應該設置largeHeap = true。如果你認爲你需要它,你應該看看你的應用程序的行爲和使用更少的內存。 –

回答

0

內存量取決於Android版本和設備型號。

public static void logHeap() { 
     Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1024*1024)); 
     Double available = new Double(Debug.getNativeHeapSize())/(1024*1024.0); 
     Double free = new Double(Debug.getNativeHeapFreeSize())/(1024*1024.0); 
     DecimalFormat df = new DecimalFormat(); 
     df.setMaximumFractionDigits(2); 
     df.setMinimumFractionDigits(2); 

     Log.d("tag", "debug. ===Heap====Heap======Heap====Heap====Heap====="); 
     Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)"); 
     Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/(1024*1024.0))) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/(1024*1024.0)))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/(1024*1024.0))) +"MB free)"); 
    } 
+0

https://gist.github.com/bepcyc/1704273 –

相關問題