3、經過Android系統提供的Runtime類,運行adb 命令(top,procrank,ps...等命令)查詢
內存耗用: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
查看每個進程及其內存情況
private void getRunningAppProcessInfo() {
mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// 得到系統里正在執行的所有進程
List<RunningAppProcessInfo> runningAppProcessesList = mActivityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcessesList) {
// 進程ID號
int pid = runningAppProcessInfo.pid;
// 用戶ID
int uid = runningAppProcessInfo.uid;
// 進程名
String processName = runningAppProcessInfo.processName;
// 佔用的內存
int[] pids = new int[] { pid };
Debug.MemoryInfo[] memoryInfo = mActivityManager
.getProcessMemoryInfo(pids);
int memorySize = memoryInfo[0].dalvikPrivateDirty;
st = st + "processName=" + processName + ",pid=" + pid + ",uid="
+ uid + ",memorySize=" + memorySize + "kb" + "\n";
System.out.println("processName=" + processName + ",pid=" + pid
+ ",uid=" + uid + ",memorySize=" + memorySize + "kb");
}
}
查看總內存:
public long getmem_TOLAL() {
long mTotal;
// /proc/meminfo讀出的內核信息進行解釋
String path = "/proc/meminfo";
String content = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(path), 8);
String line;
if ((line = br.readLine()) != null) {
content = line;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// beginIndex
int begin = content.indexOf(':');
// endIndex
int end = content.indexOf('k');
// 截取字符串信息
content = content.substring(begin + 1, end).trim();
mTotal = Integer.parseInt(content);
return mTotal;
}
查看內存信息(該api較新):
public long getmem_UNUSED(Context mContext) {
long MEM_UNUSED;
// 獲得ActivityManager
ActivityManager am = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
// 建立ActivityManager.MemoryInfo對象
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
textView3.setText("totalMen:" + mi.totalMem / 1024 + "\n"
+ "threshold:" + mi.threshold / 1024 + "\n" + "availMem:"
+ mi.availMem / 1024 + "\n");
// 取得剩餘的內存空間
MEM_UNUSED = mi.availMem / 1024;
return MEM_UNUSED;
}
查看app內存:
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
int i=manager.getMemoryClass();
textView.setText("\n"+"app:"+i);
像Linux這樣的現代操做系統的內存使用是很是複雜的。所以很是難準確的知道你的應用程序使用了好多內存。
查看內存使用的方式有很是多種。但是各個方式查看到的結果可能會有微略不一樣。
方式一,
Running services
經過手機上Running services的Activity查看,可以經過Setting->Applications->Running services進。
ActivityManager.getMemoryInfo()主要是用於獲得當前系統剩餘內存的及推斷是否處於低內存執行。
實例1:
private void displayBriefMemory() {
final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
Log.i(tag,"系統剩餘內存:"+(
info.availMem
>> 10)+"k");
Log.i(tag,"系統是否處於低內存執行:"+
info.lowMemory
);
Log.i(tag,"當系統剩餘內存低於"+
info.threshold
+"時就當作低內存執行");
}
ActivityManager.getMemoryInfo()是用ActivityManager.MemoryInfo返回結果,而不是Debug.MemoryInfo。他們不同的。
ActivityManager.MemoryInfo僅僅有三個Field:
availMem:表示
系統剩餘內存
lowMemory:它是boolean值,表示
系統是否處於低內存執行
hreshold:它表示
當系統剩餘內存低於好多時就當作低內存執行
方式三。在代碼中使用Debug的getMemoryInfo(Debug.MemoryInfo memoryInfo)或ActivityManager的MemoryInfo[] getProcessMemoryInfo(int[] pids)
該方式獲得的MemoryInfo所描寫敘述的內存使用狀況比較具體.數據的單位是
KB
.
MemoryInfo的Field例如如下
dalvikPrivateDirty
:
The private dirty pages used by dalvik。
dalvikPss
:
The proportional set size for dalvik.
dalvikSharedDirty
:
The shared dirty pages used by dalvik.
nativePrivateDirty
:
The private dirty pages used by the
native heap
.
nativePss
:
The proportional set size for the native heap.
nativeSharedDirty
:
The shared dirty pages used by the
native heap.
otherPrivateDirty
:
The private dirty pages used by everything else.
otherPss
:
The proportional set size for everything else.
otherSharedDirty
:
The shared dirty pages used by everything else.
Android和Linux同樣有大量內存在進程之間進程共享。某個進程準確的使用好多內存其實是很是難統計的。
因爲有paging out to disk(換頁)。因此假設你把所有映射到進程的內存相加,它可能大於你的內存的實際物理大小。
native:是被native堆使用的內存。應該指使用C\C++在堆上分配的內存。
other:是指除
dalvik和
native使用的內存。但是詳細是指什麼呢?至少包含在C\C++分配的非堆內存,比方分配在棧上的內存。puzlle!
share:是指共享的內存。
PSS:
實際使用的物理內存(比例分配共享庫佔用的內存)
Pss:它是把共享內存依據必定比例分攤到共享它的各個進程來計算所獲得進程使用內存。網上又說是比例分配共享庫佔用的內存。那麼至於這裏的共享是否僅僅是庫的共享,仍是不清楚。
PrivateDirty:它是指非共享的,又不能換頁出去(
can not be paged to disk )的內存的大小。比方Linux爲了提升分配內存速度而緩衝的小對象,即便你的進程結束,該內存也不會釋放掉。它僅僅是又又一次回到緩衝中而已。
SharedDirty:參照
PrivateDirty我以爲
它應該是指共享的,又不能換頁出去(
can not be paged to disk )的內存的大小。比方Linux爲了提升分配內存速度而緩衝的小對象,即便所有共享它的進程結束,該內存也不會釋放掉,它僅僅是又又一次回到緩衝中而已。
詳細代碼請參考實例1
注意1:
MemoryInfo所描寫敘述的內存使用狀況都可以經過命令adb shell "dumpsys meminfo %curProcessName%" 獲得。
注意2:假設想在代碼中同一時候獲得多個進程的內存使用或非本進程的內存使用狀況請使用
ActivityManager的MemoryInfo[] getProcessMemoryInfo(int[] pids),
不然
Debug的getMemoryInfo(Debug.MemoryInfo memoryInfo)就可以了。
注意3:可以經過ActivityManager的
List<<a href="http://developer.android.com/reference/android/app/ActivityManager.RunningAppProcessInfo.html" rel="nofollow" style="color: rgb(207, 121, 28); text-decoration: none;">ActivityManager.RunningAppProcessInfo>
getRunningAppProcesses()
獲得當前所有執行的進程信息。
注意4:
數據的單位是KB.
方式四、使用Debug的getNativeHeapSize ()。getNativeHeapAllocatedSize ()。getNativeHeapFreeSize ()方法。
該方式僅僅能獲得Native堆的內存大概狀況,數據單位爲字節。
Returns the amount of allocated memory in the native heap.
返回的是當前進程navtive堆中
已使用的內存大小
static long
getNativeHeapFreeSize()
Returns the amount of free memory in the native heap.
返回的是當前進程navtive堆中已經
剩餘的內存大小
static long
getNativeHeapSize()
Returns the size of the native heap.
返回的是當前進程navtive堆自己
總的內存大小
演示樣例代碼:
Log.i(tag,"NativeHeapSizeTotal:"+(Debug.getNativeHeapSize()>>10));
Log.i(tag,"NativeAllocatedHeapSize:"+(Debug.getNativeHeapAllocatedSi
ze()>>10));
Log.i(tag,"NativeAllocatedFree:"+(Debug.getNativeHeapFreeSize()>>10));
注意
:DEBUG中居然沒有與上面相相應的關於dalvik的函數。
方式五
、使用
dumpsys meminfo命令。
咱們可以在adb shell 中執行dumpsys meminfo命令來獲得進程的內存信息。在該命令的後面要加上進程的名字。以肯定是哪一個進程。
比方
"adb shell dumpsys meminfo
com.teleca.robin.test
" 將獲得com.teleca.robin.test進程使用的內存的信息:
Applications Memory Usage (kB):
Uptime: 12101826 Realtime: 270857936
** MEMINFO in pid 3407 [com.teleca.robin.test] **
native
dalvik
other
total
size
: 3456 3139 N/A 6595
allocated
: 3432 2823 N/A 6255
free
: 23 316 N/A 339
(Pss)
: 724 1101 1070 2895
(shared dirty)
: 1584 4540 1668 7792
(priv dirty)
: 644 608 688 1940
Objects
Views:
0
ViewRoots:
0
AppContexts:
0
Activities:
0
Assets:
3
AssetManagers:
3
Local Binders:
5
Proxy Binders:
11
Death Recipients:
0
OpenSSL Sockets:
0
SQL
heap:
0
memoryUsed:
0
pageCacheOverflo:
0
largestMemAlloc:
0
Asset Allocations
zip:/data/app/com.teleca.robin.test-1.apk:/resources.arsc: 1K
"size" 表示的是總內存大小(kb)。, "allocated" 表示的是已使用了的內存大小(kb),, "free"表示的是剩餘的內存大小(kb), 不少其它的可以參照方式三和方式四中的描寫敘述
方式6、
使用
"adb shell procrank"命令
假設你想查看所有進程的內存使用狀況。可以使用"adb shell procrank"命令。命令返回將例如如下:
PID
Vss
Rss
Pss
Uss
cmdline
188 75832K 51628K 24824K 19028K system_server
308 50676K 26476K 9839K 6844K system_server
2834 35896K 31892K 9201K 6740K com.sec.android.app.twlauncher
265 28536K 28532K 7985K 5824K com.android.phone
100 29052K 29048K 7299K 4984K zygote
258 27128K 27124K 7067K 5248K com.swype.android.inputmethod
270 25820K 25816K 6752K 5420K com.android.kineto
1253 27004K 27000K 6489K 4880K com.google.android.voicesearch
2898 26620K 26616K 6204K 3408K com.google.android.apps.maps:FriendService
297 26180K 26176K 5886K 4548K com.google.process.gapps
3157 24140K 24136K 5191K 4272K android.process.acore
2854 23304K 23300K 4067K 2788K com.android.vending
3604 22844K 22840K 4036K 3060K com.wssyncmldm
592 23372K 23368K 3987K 2812K com.google.android.googlequicksearchbox
3000 22768K 22764K 3844K 2724K com.tmobile.selfhelp
101 8128K 8124K 3649K 2996K /system/bin/mediaserver
3473 21792K 21784K 3103K 2164K com.android.providers.calendar
3407 22092K 22088K 2982K 1980K com.teleca.robin.test
2840 21380K 21376K 2953K 1996K com.sec.android.app.controlpanel
......................................................................................................................
注意1:這裏的PSS和方式四PSS的total並不一致,有細微的區別。爲何呢?這是因爲procrank 命令和meminfo命令使用的內核機制不太同樣,因此結果會有細微區別
注意2:這裏的Uss 和方式四的Priv Dirtyd的total差點兒相等.他們彷佛表示的是同一個意義。但是現在獲得的關於它們的意義的解釋卻不太一樣。難道這裏Private的都是dirty(這裏指不能換頁)? Puzzle!
方式7、使用
"adb shell cat /proc/meminfo" 命令。
MemTotal: 395144 kB
MemFree: 184936 kB
Buffers: 880 kB
Cached: 84104 kB
SwapCached: 0 kB
................................................................................................
MemTotal :可供系統和用戶使用的總內存大小 (它比實際的物理內存要小,因爲還有些內存要用於radio, DMA buffers, 等).
MemFree:剩餘的可用內存大小。這裏該值比較大。實際上通常Android system 的該值一般都很是小,因爲咱們儘可能讓進程都保持執行。這樣會耗掉大量內存。
Cached: 這個是系統用於文件緩衝等的內存. 一般systems需要20MB 以免bad paging states;。當內存緊張時。the Android out of memory killer將殺死一些background進程。以免他們消耗過多的cached RAM ,固然假設下次再用到他們,就需要paging. 那麼是說background進程的內存包括在該項中嗎?
方式八,使用「adb shell ps -x」命令
該方式主要獲得的是內存信息是VSIZE 和RSS。
USER PID PPID VSIZE RSS WCHAN PC NAME
.........................省略.................................
app_70 3407 100 267104 22056 ffffffff afd0eb18 S com.teleca.robin.test (u:55, s:12)
app_7 3473 100 268780 21784 ffffffff afd0eb18 S com.android.providers.calendar (u:16, s:8)
radio 3487 100 267980 21140 ffffffff afd0eb18 S com.osp.app.signin (u:11, s:12)
system 3511 100 273232 22024 ffffffff afd0eb18 S com.android.settings (u:11, s:4)
app_15 3546 100 267900 20300 ffffffff afd0eb18 S com.sec.android.providers.drm (u:15, s:6)
app_59 3604 100 272028 22856 ffffffff afd0eb18 S com.wssyncmldm (u:231, s:54)
root 4528 2 0 0 c0141e4c 00000000 S flush-138:13 (u:0, s:0)
root 4701 152 676 336 c00a68c8 afd0e7cc S /system/bin/sh (u:0, s:0)
root 4702 4701 820 340 00000000 afd0d8bc R ps (u:0, s:5)
注意1:由於RSS的價值不是很是大。因此通常不用。
實例1
int cnt=0;
final static int kBufferMinSize=1000;
final static int kBufferMaxSize=2000;
StringBuffer strBuffer=new StringBuffer(kBufferMinSize);
StringBuffer strBuffer2=new StringBuffer(kBufferMinSize);
StringBuffer strBuffer3=new StringBuffer(kBufferMinSize);
StringBuffer strBufferNativePss=new StringBuffer(kBufferMinSize);
StringBuffer strBufferDalvikPss=new StringBuffer(kBufferMinSize);
StringBuffer strBufferOtherPss=new StringBuffer(kBufferMinSize);
Debug.MemoryInfo memoryInfo=new Debug.MemoryInfo();
final static String tag="robin";
void printMemory()
{
long totalMemory=Runtime.getRuntime().totalMemory();
long freeMemory=Runtime.getRuntime().freeMemory();
long usedMemory=(totalMemory-freeMemory)>>10;
totalMemory=totalMemory>>10;
freeMemory=freeMemory>>10;
if(strBuffer.length()>kBufferMaxSize)
{
strBuffer.delete(0,strBuffer.length());
strBuffer2.delete(0,strBuffer2.length());
strBuffer3.delete(0,strBuffer3.length());
strBufferNativePss.delete(0,strBufferNativePss.length());
strBufferDalvikPss.delete(0,strBufferDalvikPss.length());
}
strBuffer.append(usedMemory+",");
strBuffer2.append(totalMemory+",");
strBuffer3.append((Debug.getNativeHeapSize()>>10)+",");
Debug.getMemoryInfo(memoryInfo);
strBufferNativePss.append((memoryInfo.nativePss)+",");
strBufferDalvikPss.append((memoryInfo.dalvikPss)+",");
if(cnt++==0)
{
Log.i(tag,"usedMemory:"+strBuffer.toString());
Log.i(tag,"totalMemory:"+strBuffer2.toString());
Log.i(tag,"NativeHeapSize:"+strBuffer3.toString());
Log.i(tag,"Native PSS:"+strBufferNativePss.toString());
Log.i(tag,"Dalvik PSS:"+strBufferDalvikPss.toString());
}
}
注意。對於輸出的內存信息日誌,咱們稍做編輯就可以用於在
excel產中圖表
。比便更直觀的進行分析
。