"Anroid Advanced" method for viewing your phone and applying memory status

Source: Internet
Author: User

we know that the Android program memory is generally limited to 16M, of course there are 24M, and the Android program memory is divided into 2 parts: native and Dalvik,dalvik is what we normally call the Java heap, we create the object is in here to assign, While bitmap is allocated directly on the native, the memory limit isNative+dalvikThe maximum limit cannot be exceeded.

1. Single App memory limit size

[Java]View Plaincopy
    1. Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service);
    2. Activitymanager.getmemoryclass ();

2. "/proc/meminfo" system memory information file "Memory condition of the whole system"

Android gets total phone memory and available memory

"/proc/meminfo" parsing


3.activitymanager.memoryinfo"entire system"

Availmem remaining memory

Threshold threshold "exceeds the secondary value to start killing background services and no associated processes"

Lowmemory Low Memory state


4.android.os.debug

Getnativeheapfreesize ()

Getnativeheapallocatedsize ()

Getnativeheapsize ()


5.Android.os. debug.memoryinfo"memory condition of current process"


6.Runtime
totalmemory ()
freememory ()
maxmemory ()

7.VMRuntime
gettargetheaputilization ()
getminimumheapsize ()
getexternalbytesallocated ()should be externally allocated memory native memory


8.what is Gc_external_alloc freed and Gc_explicit freed?

memory hints released by the system GC
this is usually shown in log: 09-28 17:16:37.543:DEBUG/DALVIKVM (21466): Gc_external_alloc Freed 390 objects/45656 bytes in 50ms
09-28 17:16:40.513:DEBUG/DALVIKVM (3267): gc_explicit freed 4501 objects/251624 bytes in 67ms
EXPLICIT:freeMemory is the memory that is used by Java in the VM--heap mem
Externa: FreeMemory is the memory allocated by malloc in the VM through the JNI native class such as Bitmap and some Cursor

In Davilk, the memory allocated to a program varies according to the manufacturer of the model, but the majority of the current is 32M, and in the VM will be divided into the memory of Java use and memory used by native, they are not shared between , That is, when your native memory is exhausted, and now Java has free memory, then native will re-like the VM application, instead of using Java directly.

For example, the above example

Explicit 3411k/6663k

External 24870k/26260k

If you need to create a 2M

Bitmap,

Native existing memory 26260-24870=1390k<2048k, so he will request memory from the VM, although the Java free memory is

6663-3411=3252>2048, but this part of the memory native is not available.

But now that you're applying for 2M of native memory, the VM will tell you that you can't allocate it because the memory you're using now is nearing the peak of 32M (26260+6663=32923), so now it's force close to Oom.

So now we're going to check the usage of our native memory to avoid oom.


ps:http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android#2299813

Third, through the runtime class provided by the Android system, execute the ADB command (Top,procrank,ps ... and other commands) query

Memory consumption: Vss/rss/pss/uss
Terms
? Vss-virtual Set Size Virtual memory consumption (contains memory consumed by shared libraries)
? Rss-resident Set Size actually uses physical memory (contains memory consumed by shared libraries)
? Pss-proportional Set Size Actual physical memory used (proportional allocation of memory consumed by shared libraries)
? Uss-unique the Set Size process consumes the physical memory alone (does not contain the memory occupied by the shared library)
In general, memory footprint has the following rules: VSS >= RSS >= PSS >= USS


View each process and its memory status

private void Getrunningappprocessinfo () {Mactivitymanager = (Activitymanager) getsystemservice (context.activity_ SERVICE)//Get all processes running in the system list<runningappprocessinfo> Runningappprocesseslist = Mactivitymanager.getrunningappprocesses (); for (Runningappprocessinfo Runningappprocessinfo: Runningappprocesseslist) {//process id number int PID = runningappprocessinfo.pid;//user Idint uid = runningappprocessinfo.uid;// Process name String processName = runningappprocessinfo.processname;//occupied memory int[] PIDs = new int[] {pid};D ebug. 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");}}
View Total Memory:

Public long Getmem_tolal () {long mtotal;///proc/meminfo read out the kernel information is interpreted by the 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! = nul L) {try {br.close ();} catch (IOException e) {e.printstacktrace ()}}} Beginindexint begin = Content.indexof (': ');//Endindexint end = Content.indexof (' k ');//intercept string information content = Content.subst Ring (begin + 1, end). Trim (); mtotal = integer.parseint (content); return mtotal;}

View memory information (the API is newer):

Public long getmem_unused (Context mcontext) {long mem_unused;//get activitymanageractivitymanager am = (activitymanager) Mcontext.getsystemservice (Context.activity_service);//Create Activitymanager.memoryinfo Object 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 ");//Get the remaining memory space mem_unused = MI.AVAILMEM/ 1024;return mem_unused;}
To View app memory:

Activitymanager manager = (Activitymanager) getsystemservice (activity_service); int I=manager.getmemoryclass (); Textview.settext ("\ n" + "app:" +i);




"Anroid Advanced" method for viewing your phone and applying memory status

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.