Android——記憶體調試

來源:互聯網
上載者:User

標籤:memory   adb   shell   dalvik   

因調試某個重大問題,懷疑到了記憶體,專門寫了個測試指令碼,記錄一下。



                                                                    撰寫不易,轉載請註明出處:http://blog.csdn.net/jscese/article/details/37928823

一.調試準備

首先需要開啟android系統的adb功能,start adbd

PC端 adb connect IP ADDRESS

如果 出現adb 異常可以嘗試 adb kill-serveradb start-server



二.adb shell指令1.查看整體記憶體

連上adb之後 可以通過 adb shell procrank 來查看當前的記憶體情況!



  • VSS - Virtual Set Size 虛擬耗用記憶體(包含共用庫佔用的記憶體)
  • RSS - Resident Set Size 實際使用實體記憶體(包含共用庫佔用的記憶體)
  • PSS - Proportional Set Size 實際使用的實體記憶體(比例分配共用庫佔用的記憶體)
  • USS - Unique Set Size 進程獨自佔用的實體記憶體(不包含共用庫佔用的記憶體)


2.查看指定進程的記憶體情況

adb shell dumpsys meminfo (包名或者PID)




3.佔用記憶體最多的進程或線程

adb shell top

顯示當前佔用最高記憶體的10個進程,adb shell top -m 10



查看線程:adb shell top -t -m 10


三.shell 指令碼

用於即時監控記憶體使用量情況,並且儲存log,我的shell script:


#!/bin/bash echo "Begain test memory">memeory_recode.txti=0while true; doadb shell procrank |grep 'RAM:'| tee -a memeory_record.txt memoryinfo=$(tail memeory_record.txt -n 1) #freememory=$memoryinfo | cut -d ' '-f 4freememory=`echo "$memoryinfo"|awk -F ' ' '{print $4}'`free=${freememory%?}if [ $free -lt 8000 ];thenecho -e "\033[31mFree Memory is $free KB Less than 8M\033[0m"| tee -a memeory_recode.txtadb shell top -m 10 -n 1 | tee -a memeory_recode.txtelseecho "freememory == $free KB"fii=$(($i+1)) sleep 1var=$(date)echo "jscese display memory at $var the  $i times"echo done

儲存RAM資訊的情況到 memeory_record.txt,並且解析freememory 的值,如果少於8000K就把佔用記憶體最高的10個進程資訊也儲存進record。


四.build.prop中的Dalvik設定

dalvik.vm.heapstartsize=8mdalvik.vm.heapgrowthlimit=96mdalvik.vm.heapsize=256mdalvik.vm.heaptargetutilization=0.75dalvik.vm.heapminfree=512kdalvik.vm.heapmaxfree=8mdalvik.vm.lockprof.threshold=500dalvik.vm.dexopt-flags=m=y

這幾個屬性代表了對dalvik的一些屬性設定,可以在/dalvik/vm/alloc/HeapSource.cpp下找到原型:

struct HeapSource {    /* Target ideal heap utilization ratio; range 1..HEAP_UTILIZATION_MAX     */    size_t targetUtilization;    /* The starting heap size.     */    size_t startSize;    /* The largest that the heap source as a whole is allowed to grow.     */    size_t maximumSize;    /*     * The largest size we permit the heap to grow.  This value allows     * the user to limit the heap growth below the maximum size.  This     * is a work around until we can dynamically set the maximum size.     * This value can range between the starting size and the maximum     * size but should never be set below the current footprint of the     * heap.     */    size_t growthLimit;    /* The desired max size of the heap source as a whole.     */    size_t idealSize;    /* The maximum number of bytes allowed to be allocated from the     * active heap before a GC is forced.  This is used to "shrink" the     * heap in lieu of actual compaction.     */    size_t softLimit;    /* Minimum number of free bytes. Used with the target utilization when     * setting the softLimit. Never allows less bytes than this to be free     * when the heap size is below the maximum size or growth limit.     */    size_t minFree;    /* Maximum number of free bytes. Used with the target utilization when     * setting the softLimit. Never allows more bytes than this to be free     * when the heap size is below the maximum size or growth limit.     */    size_t maxFree;...}


大體對應的意思如下:

1.heapstartsize——堆初始分配的大小,一個app啟動的時候分配的記憶體大小

2.heapgrowthlimit——分配的一個堆最大的增長值,一個app最多分配的記憶體大小,超出的話應該會報outofmemory

3.heapsize——整個堆所能達到的最大值,也就是應用程式所能用的記憶體總和

4.heaptargetutilization——代表堆的利用率,實際使用與最大利用對比

5.heapminfree——堆大小的限制因素,在堆的大小沒超過限定值的情況下 最小的空閑值

6.heapmaxfree——和最小相反,堆中最多能閒置大小

7.lockprof.threshold——調試記錄程式內部鎖資源爭奪的閾值,預設值是500

8.dexopt-flags——程式碼的校正與最佳化,以下來自百科:

dalvik.vm.dexopt-flags:本參數控制Dalvik虛擬機器的程式碼校正和最佳化。可填寫的值有m、v和o。 m為標準選項,可以是m=y或m=n。若m=y則啟用不安全的程式碼的校正和Managed 程式碼的最佳化。相容性和安全性最高,推薦使用。 v為校正選項,可與o並存。可以是v=a或v=n。若v=a則表示校正所有代碼,v=n則關閉代碼的校正。 o為最佳化選項,可與v並存。可以是o=v或o=a。若o=v則表示最佳化以校正過的代碼,o=a則表示最佳化所有代碼。 例如: dalvik.vm.dexopt-flags=m=y dalvik.vm.dexopt-flags=v=n,o=v注意,這個參數只會影響到安裝APK之後或初次使用APK時產生dex檔案時有效。若整個系統(包括應用程式)為odex化,則無意義。






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.