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<runningappprocessinfo> 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 ()); } <div class= "Dp-highlighter bg_java" ><div class= "bar" > <div class= "Tools" ><b>[java]</b > <a href= "#" class= "Viewsource" title= "View Plain" onclick= "Dp.sh.Toolbar.Command (' Viewsource ', this); false; " > View Plain</a><a href= "# "class=" CopyToClipboard "title=" Copy onclick= "Dp.sh.Toolbar.Command (' CopyToClipboard ', this); > Copy</a><a href= "#" class= "Printsource" title= "print" onclick= "Dp.sh.Toolbar.Command (' Printsource ', this); return false; >print</a><a href= "#" class= "about" title= "?" onclick= "Dp.sh.Toolbar.Command (' about ', this); return false ;" >?</a> <div style= "Position:absolute; left:0px; top:0px; width:0px; height:0px; z-index:99; "> <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&width=0& Height=0 "wmode=" Transparent "> </div> </div> </div> <ol start=" 1 "class=" Dp-j "><li class= "alt" ><span><span class= "keyword" >return</span> <span> </span>
<span class= "Number" >0</span><span>; </span></span></li><li class= "" ><span> </span></li></ol> </div><pre name= "code" class= "java" style= "Display:none; "> return 0;} </pre> <pre></pre> <br> <p> translation: "Pss", "Privatedirty" and "Shareddirty" What is the difference between </p> < p> 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. </p> <p> 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.
</p> <p> 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. </p> <p> 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), [<img alt= "smiles" src= "Smile.gif" > Attempt was:-)]). <br&Gt </p> <p> original <br> </p> <p>but as to what the difference is between "Pss", "Privatedirty", "and" Shareddirty "... ok now the fun begins.</p> <p>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.</p>
; <p>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.</p> <p>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.</p> <p>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) .</p> <p></p> <p><br> </p> <P&G
t;<br> </p> <p><br> </p> <p><br> </p> <p><br> </p>
<br>