歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 查看基於Android 系統單個進程內存、CPU使用情況的幾種方法

查看基於Android 系統單個進程內存、CPU使用情況的幾種方法

日期:2017/3/1 10:57:45   编辑:Linux編程

一、利用Android API函數查看
1.1 ActivityManager查看可用內存。
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(outInfo);
outInfo.availMem即為可用空閒內存。
1.2、android.os.Debug查詢PSS,VSS,USS等單個進程使用內存信息
MemoryInfo[] memoryInfoArray = am.getProcessMemoryInfo(pids);
MemoryInfo pidMemoryInfo=memoryInfoArray[0];
pidMemoryInfo.getTotalPrivateDirty();

getTotalPrivateDirty()
Return total private dirty memory usage in kB. USS

getTotalPss()
Return total PSS memory usage in kB.
PSS
getTotalSharedDirty()
Return total shared dirty memory usage in kB. RSS


二、直接對Android文件進行解析查詢,
/proc/cpuinfo系統CPU的類型等多種信息。
/proc/meminfo 系統內存使用信息

/proc/meminfo
MemTotal: 16344972 kB
MemFree: 13634064 kB
Buffers: 3656 kB
Cached: 1195708 kB
我們查看機器內存時,會發現MemFree的值很小。這主要是因為,在linux中有這麼一種思想,內存不用白不用,因此它盡可能的cache和buffer一些數據,以方便下次使用。但實際上這些內存也是可以立刻拿來使用的。
所以 空閒內存=free+buffers+cached=total-used
通過讀取文件/proc/meminfo的信息獲取Memory的總量。
ActivityManager. getMemoryInfo(ActivityManager.MemoryInfo)獲取當前的可用Memory量。

三、通過Android系統提供的Runtime類,執行adb 命令(top,procrank,ps...等命令)查詢
通過對執行結果的標准控制台輸出進行解析。這樣大大的擴展了Android查詢功能.例如:
final Process m_process = Runtime.getRuntime().exec("/system/bin/top -n 1");
final StringBuilder sbread = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(m_process.getInputStream()), 8192);

# procrank
Runtime.getRuntime().exec("/system/xbin/procrank");
內存耗用:VSS/RSS/PSS/USS
Terms
• VSS - Virtual Set Size 虛擬耗用內存(包含共享庫占用的內存)
• RSS - Resident Set Size 實際使用物理內存(包含共享庫占用的內存)
• PSS - Proportional Set Size 實際使用的物理內存(比例分配共享庫占用的內存)
• USS - Unique Set Size 進程獨自占用的物理內存(不包含共享庫占用的內存)
一般來說內存占用大小有如下規律:VSS >= RSS >= PSS >= USS
USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.

Copyright © Linux教程網 All Rights Reserved