十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
今天就跟大家聊聊有關(guān)怎么在Android中獲取內(nèi)存使用情況,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.代碼獲取當前app內(nèi)存的使用情況
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//大分配內(nèi)存
int memory = activityManager.getMemoryClass();
System.out.println("memory: "+memory);
//大分配內(nèi)存獲取方法2
float maxMemory = (float) (Runtime.getRuntime().maxMemory() * 1.0/ (1024 * 1024));
//當前分配的總內(nèi)存
float totalMemory = (float) (Runtime.getRuntime().totalMemory() * 1.0/ (1024 * 1024));
//剩余內(nèi)存
float freeMemory = (float) (Runtime.getRuntime().freeMemory() * 1.0/ (1024 * 1024));
System.out.println("maxMemory: "+maxMemory);
System.out.println("totalMemory: "+totalMemory);
System.out.println("freeMemory: "+freeMemory);結(jié)果
System.out: memory: 256 System.out: maxMemory: 256.0 System.out: totalMemory: 11.974937 System.out: freeMemory: 3.6257935
這說明我這個app在當前手機的大分配內(nèi)存是256m,現(xiàn)在已經(jīng)分配了11m,這11m中有6m是空閑的
當然通過Monitors可以更直觀的查看內(nèi)存使用情況
2.使用dos命令
(1)打開dos窗口,執(zhí)行adb shell
(2)dumpsys meminfo 包名
結(jié)果:

3.使用Monitors或者DDMS

monitors

看完上述內(nèi)容,你們對怎么在Android中獲取內(nèi)存使用情況有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。