Android Get Process Memory usage method

Source: Internet
Author: User
Tags memory usage stringbuffer
 Activitymanager Activitymanager = (activitymanager) context.getsystemservice (ACTIVITY_
SERVICE);
Memoryinfo memoryinfo = new Activitymanager.memoryinfo ();

Activitymanager.getmemoryinfo (Memoryinfo);
LOG.I (TAG, "Memoryinfo.availmem" + Memoryinfo.availmem + "n");
LOG.I (TAG, "memoryinfo.lowmemory" + memoryinfo.lowmemory + "n");

LOG.I (TAG, "memoryinfo.threshold" + Memoryinfo.threshold + "n");

list<runningappprocessinfo> runningappprocesses = activitymanager.getrunningappprocesses ();
Map<integer, string> pidmap = new Treemap<integer, string> (); for (Runningappprocessinfo runningappprocessinfo:runningappprocesses) {pidmap.put (Runningappprocessinfo.pid, Runni
Ngappprocessinfo.processname);

} collection<integer> keys = Pidmap.keyset ();
    for (int key:keys) {int pids[] = new INT[1];
    Pids[0] = key;
    android.os.debug.memoryinfo[] Memoryinfoarray = Activitymanager.getprocessmemoryinfo (PIDs); for (Android.os.Debug.MemoRyinfo Pidmemoryinfo:memoryinfoarray) {log.i (TAG, String.Format ("* * meminfo in PID%d [%s] **n", PIDS[0],PIDM
        Ap.get (Pids[0]));
        LOG.I (TAG, "Pidmemoryinfo.gettotalprivatedirty ():" + pidmemoryinfo.gettotalprivatedirty () + "n");
        LOG.I (TAG, "PIDMEMORYINFO.GETTOTALPSS ():" + PIDMEMORYINFO.GETTOTALPSS () + "n");
    LOG.I (TAG, "Pidmemoryinfo.gettotalshareddirty ():" + pidmemoryinfo.gettotalshareddirty () + "n"); }
}

Before you see the above code, you have written similar code according to the API instructions as follows, including more output parameters. If you want to check the memory usage of other processes, you can omit the conditional judgment in the loop. According to Stackoverflow.com's post, the value of PSS is the best indication that the process uses the memory state

Public long getmem_self () {Activitymanager am = (activitymanager) getsystemservice (Context.activity_service);
        list&lt;runningappprocessinfo&gt; ProcInfo = am.getrunningappprocesses (); for (Runningappprocessinfo runningappprocessinfo:procinfo) {System.out.println (runningappprocessinfo.process  
        	Name+ String.Format (", pid =%d", runningappprocessinfo.pid)); if (RunningAppProcessInfo.processName.indexOf (This.getpackagename ())!=-1) {int pids[] = {Runningapp
        		Processinfo.pid};
        		Debug.memoryinfo self_mi[] = Am.getprocessmemoryinfo (PIDs);
        		StringBuffer strbuf = new StringBuffer (); Strbuf.append ("proccess Name:"). Append (Runningappprocessinfo.processname). Append ("n pid:"). Append (runningappp rocessinfo.pid). Append ("n dalvikprivatedirty:"). Append (Self_mi[0].dalvikprivatedirty). Append ("N DALVIKP SS: "). Append (SELF_MI[0].DALVIKPSS). Append (" n dalvikshareddirty: "). AppEnd (Self_mi[0].dalvikshareddirty). Append ("n nativeprivatedirty:"). Append (Self_mi[0].nativeprivatedirty). AppE nd ("n nativepss:"). Append (SELF_MI[0].NATIVEPSS). Append ("n nativeshareddirty:"). Append (self_mi[0). Nativeshareddirty). Append ("n otherprivatedirty:"). Append (Self_mi[0].otherprivatedirty). Append ("n otherpss:") . Append (SELF_MI[0].OTHERPSS). Append ("n othershareddirty:"). Append (Self_mi[0].othershareddirty). Append ("N Tot Alprivatedirty: "). Append (Self_mi[0].gettotalprivatedirty ()). Append (" n totalpss: "). Append (Self_mi[0].gettotalpss
        		()). Append ("n totalshareddirty:"). Append (Self_mi[0].gettotalshareddirty ());
        	LOG.V ("TEST", strbuf.tostring ()); } &lt;div class= "Dp-highlighter bg_java" &gt;&lt;div class= "bar" &gt; &lt;div class= "Tools" &gt;&lt;b&gt;[java]&lt;/b &gt; &lt;a href= "#" class= "Viewsource" title= "View Plain" onclick= "Dp.sh.Toolbar.Command (' Viewsource ', this); false; " &gt; View Plain&lt;/a&gt;&lt;a href= "# "class=" CopyToClipboard "title=" Copy onclick= "Dp.sh.Toolbar.Command (' CopyToClipboard ', this); &gt; Copy&lt;/a&gt;&lt;a href= "#" class= "Printsource" title= "print" onclick= "Dp.sh.Toolbar.Command (' Printsource ', this); return false; &gt;print&lt;/a&gt;&lt;a href= "#" class= "about" title= "?" onclick= "Dp.sh.Toolbar.Command (' about ', this); return false ;" &gt;?&lt;/a&gt; &lt;div style= "Position:absolute; left:0px; top:0px; width:0px; height:0px; z-index:99; "&gt; &lt;embed id=" zeroclipboardmovie_3 "src=" http://static.blog.csdn.net/scripts/ZeroClipboard/ Zeroclipboard.swf "loop= false" menu= "false" quality= "Best" bgcolor= "#ffffff" width= "0" height= "0" name= " Zeroclipboardmovie_3 "align=" Middle "allowscriptaccess=" Always "allowfullscreen=" false "Type=" application/ X-shockwave-flash "pluginspage=" http://www.macromedia.com/go/getflashplayer "flashvars=" id=3&amp;width=0&amp; Height=0 "wmode=" Transparent "&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;ol start=" 1 "class=" Dp-j "&GT;&LT;li class= "alt" &gt;&lt;span&gt;&lt;span class= "keyword" &gt;return&lt;/span&gt; &lt;span&gt;&nbsp;&lt;/span&gt; 
&lt;span class= "Number" &gt;0&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class= "" &gt;&lt;span&gt; &nbsp; &lt;/span&gt;&lt;/li&gt;&lt;/ol&gt; &lt;/div&gt;&lt;pre name= "code" class= "java" style= "Display:none; "&gt; return 0;} &lt;/pre&gt; &lt;pre&gt;&lt;/pre&gt; &lt;br&gt; &lt;p&gt; translation: "Pss", "Privatedirty" and "Shareddirty" What is the difference between &lt;/p&gt; &lt; p&gt; Android (ie Linux), where a lot of memory is actually shared by multiple processes, it's not clear how much memory a process actually consumes. It's not even clear which paging is added to the disk. &lt;/p&gt; &lt;p&gt; &nbsp; So, if you want to get all the physical memory values that are actually mapped in each process, and then try to add a total sum, you may get a value much larger than the actual amount of memory.
&lt;/p&gt; &lt;p&gt; &nbsp; PSS is a kernel computing metric that considers shared memory-basically, each memory page of a process is scaled down by one ratio, which is related to the number of other processes that use the page as well. Theoretically you can accumulate the PSS consumption of all processes to check the memory footprint of all processes, or you can compare the PSS of the process to find out the respective weights of the process. &lt;/p&gt; &lt;p&gt; &nbsp; Another interesting parameter is Privatedirty, which is essentially memory that cannot be paged to disk in the process, and is not shared with other processes. Another way to view the memory usage of a process is when the system's available memory changes (and may soon be incorporated into the cache or other processes that use that memory area when the process ends), [&lt;img alt= "smiles" src= "Smile.gif" &gt; Attempt was:-)]). &lt;br&Gt &lt;/p&gt; &lt;p&gt; original &lt;br&gt; &lt;/p&gt; &lt;p&gt;but as to what the difference is between "Pss", "Privatedirty", "and" Shareddirty "... ok now the fun begins.&lt;/p&gt; &lt;p&gt;a lot of memory in Android (and Linux systems in general) Actually shared across multiple processes. So much memory a processes uses is really not clear. Add on, paging out to disk (let alone swap which we don ' t use on Android) and it is even less clear.&lt;/p&gt
; &lt;p&gt;thus If you are were to take all of the physical RAM actually mapped into each process, and add all of the Proce  SSEs, you would probably end/a number much greater than the actual total ram.&lt;/p&gt; &lt;p&gt;the Pss number is  A metric the kernel computes that takes to account memory sharing--basically each page of RAM in a process is scaled By a ratio of the number of other processes also using that page. This is way can (in theory) add up the PSS across all processes to the total RAM they aRe using, and compare PSS between processes to get a rough idea of their relative weight.&lt;/p&gt; &lt;p&gt;the Teresting metric is privatedirty, which are basically the amount of RAM inside the process that can and not being paged to D ISK (it isn't backed by the "same data on disk"), and is isn't shared with any other processes. Another way to look at the ' is ' RAM that would become available to the system while that process goes away (and probabl Y quickly subsumed into caches and uses of it) .&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;br&gt; &lt;/p&gt; &LT;P&G
t;&lt;br&gt; &lt;/p&gt; &lt;p&gt;&lt;br&gt; &lt;/p&gt; &lt;p&gt;&lt;br&gt; &lt;/p&gt; &lt;p&gt;&lt;br&gt; &lt;/p&gt;
 &lt;br&gt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.