一、利用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.
四、dumpsys
dumpsys meminfo
Applications Memory Usage (kB):
Uptime: 1031937 Realtime: 1167591179860
Total PSS by process:
44049 kB: com.skyworth.launchersky_app_home (pid 911)
16839 kB: system (pid 791)
12835 kB: com.skyworth.standardservices (pid 1310)
11233 kB: com.android.wallpaper (pid 881)
9791 kB: com.skyworth.sky_app_atv (pid 1018)
9782 kB: android.process.media (pid 1232)
9622 kB: com.skyworth.hotkey (pid 1666)
9279 kB: com.android.systemui (pid 866)
7400 kB: com.android.email (pid 1265)
7318 kB: com.mstar.tv.service (pid 1246)
6980 kB: com.skyworthdigital.stb.dataprovider (pid 1335)
5808 kB: com.android.exchange (pid 1286)
4923 kB: com.android.inputmethod.pinyin (pid 892)
4351 kB: com.android.providers.calendar (pid 985)
4132 kB: com.android.calendar (pid 961)
3548 kB: com.android.deskclock (pid 1059)
Total PSS by OOM adjustment:
16839 kB: System
16839 kB: system (pid 791)
9279 kB: Persistent
9279 kB: com.android.systemui (pid 866)
44049 kB: Foreground
44049 kB: com.skyworth.launchersky_app_home (pid 911)
11233 kB: Visible
11233 kB: com.android.wallpaper (pid 881)
25076 kB: Perceptible
12835 kB: com.skyworth.standardservices (pid 1310)
7318 kB: com.mstar.tv.service (pid 1246)
4923 kB: com.android.inputmethod.pinyin (pid 892)
6980 kB: A Services
6980 kB: com.skyworthdigital.stb.dataprovider (pid 1335)
9791 kB: Previous
9791 kB: com.skyworth.sky_app_atv (pid 1018)
44643 kB: Background
9782 kB: android.process.media (pid 1232)
9622 kB: com.skyworth.hotkey (pid 1666)
7400 kB: com.android.email (pid 1265)
5808 kB: com.android.exchange (pid 1286)
4351 kB: com.android.providers.calendar (pid 985)
4132 kB: com.android.calendar (pid 961)
3548 kB: com.android.deskclock (pid 1059)
Total PSS by category:
56161 kB: Dalvik
30951 kB: Native
28795 kB: Unknown
24122 kB: .so mmap
18489 kB: .dex mmap
7047 kB: Other mmap
1109 kB: .ttf mmap
1036 kB: .apk mmap
88 kB: Other dev
52 kB: Ashmem
24 kB: .jar mmap
16 kB: Cursor
Total PSS: 167890 kB
轉自:http://blog.csdn.net/kieven2008/article/details/6445421