2012-03-29 53 views
5

有什麼辦法,如何確定實際分配的內存在Android應用程序(代碼中)?Android - 獲得分配內存

感謝

航點

+0

航點,看到這個答案可能會有所幫助[閱讀Android設備總數ra m與/proc/meminfo](http://stackoverflow.com/questions/9869761/read-android-device-total-ammount-of-ram-with-proc-meminfo/9870018#9870018) – 2012-03-29 05:52:02

+1

我只得到總數我需要實際使用我的應用程序 – Waypoint 2012-03-29 09:51:45

+0

運行此命令'Runtime.getRuntime()。exec(「/ system/bin/top -n 50」);'這會給你所有進程的信息,然後解析它 – 2012-03-29 10:00:39

回答

3

如果你在談論Android應用程序內存中,然後,

ActivityManager.getMemoryInfo()是我們看整體內存使用率最高級別的API。

在較低級別上,可以使用Debug API來獲取有關內存使用情況的原始內核級別信息。

注意從2.0開始,還有一個API ActivityManager.getProcessMemoryInfo,以獲取有關另一個進程的此信息。

你也可以解析/proc/meminfo命令。並獲得內存信息的迴應。

編輯:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
MemoryInfo memoryInfo = new MemoryInfo(); 
activityManager.getMemoryInfo(memoryInfo); 
Log.i("memory free", "" + memoryInfo.availMem); 

欲瞭解更多信息,看看這兩款SO問題:

  1. How to discover memory usage of my application in Android
  2. How to get current memory usage in android?
+0

謝謝,但是ActivityManager.getMemoryInfo()究竟是什麼mehotd?沒有人似乎對我很冷漠 – Waypoint 2012-03-29 05:26:34

+0

感謝您的編輯,我無法解析proc/meminfo,所以我嘗試使用ActivityManager.getMemoryInfo,但它只顯示availMem。儘管如此,我加載領域與否,availMem不會改變 – Waypoint 2012-03-29 05:42:31

+0

在這個問題http://stackoverflow.com/questions/3118234/how-to-get-memory-usage-and-cpu-usage-in-android use/proc/meminfo文件獲取內存信息。 – user370305 2012-03-29 05:46:06