Android basics 15: Obtain Android information 05-memory and CPU usage of a single process

Source: Internet
Author: User

This article describes how to obtain the memory and CPU usage information of a single process.

Memory consumption: VSS/RSS/PSS/USS

Terms
• VSS-virtual set size virtual memory consumption (including memory occupied by shared libraries)
• RSS-resident set size actually uses physical memory (including memory occupied by shared libraries)
• Pss-proportional set size physical memory actually used (proportional allocation of memory occupied by the Shared Library)
• Uss-unique set size physical memory occupied by the process (excluding the memory occupied by the Shared Library)
Generally, the memory usage is as follows: VSS> = RSS> = PSS> = USS

USS is the total private memory for a process, I. e. that memory that is completely unique to that process. USS is an extremely useful number because it indicates the true incremental cost of running a special process. when a process is killed, the USS
Is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.

1. Use Android API functions to view
1.1 activitymanager view available memory.

ActivityManager.MemoryInfo utInfo = new ActivityManager.MemoryInfo(); am.getMemoryInfo(outInfo); 

Outinfo. availmem is available idle memory.
1.2. Android. OS. debug queries memory usage information of a single process, such as PSS, VSS, and USS.

MemoryInfo[] memoryInfoArray = am.getProcessMemoryInfo(pids); MemoryInfo pidMemoryInfo=memoryInfoArray[0];pidMemoryInfo.getTotalPrivateDirty();

Gettotalprivatedirty ()
Return total private dirty memory usage in KB.
USS
Gettotalpss ()
Return total PSS memory usage in KB.
Pss
Gettotalshareddirty ()
Return total shared dirty memory usage in KB.
RSS

2. parse and query Android files directly,
/Proc/cpuinfo system CPU type and other information.
/Proc/meminfo system memory usage information
For example
/Proc/meminfo
Memtotal: 16344972 KB
Memfree: 13634064 KB
Buffers': 3656 KB
Cached: 1195708 KB
When we look at the machine memory, we will find that the memfree value is very small. This is mainly because there is such an idea in Linux, the memory does not need to be white, so it tries its best to cache and buffer some data for the next use. But in fact, these memories can also be used immediately.
So free memory = free + buffers + cached = total-used
Obtain the total amount of memory by reading the/proc/meminfo file.
Activitymanager. getmemoryinfo (activitymanager. memoryinfo) gets the current amount of available memory.

3. Run the ADB command (top, procrank, PS..., and other commands) to query the runtime class provided by the Android system.
Parse the standard console output of the execution result. This greatly extends the android query function. For example:
Final process m_process = runtime.getruntime(cmd.exe C ("/system/bin/top-N 1 ");
Final stringbuilder sbread = new stringbuilder ();
Bufferedreader = new bufferedreader (New inputstreamreader (m_process.getinputstream (), 8192 );
# Procrank
Runtime.getruntime(cmd.exe C ("/system/xbin/procrank ");

Related Article

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.