How to get memory info and thread list from adb in Android

This article will introduce how to get the processes and threads running on a device from adb, as well as to get information about memory usage.

You can use the following commands to get these information:

  • adb shell ps -ef : prints the list of running processes
  • adb shell ps -T : prints the list of running threads
  • adb shell dumpsys meminfo [package name|pid] : prints a snapshot of the memory usage of the running process

Let's take a look at each command with an example.

1. "ps" command from adb

The adb shell provides a ps command like Linux. With the ps command, you can print out a list of processes and threads.

$ adb shell ps --help
usage: ps [-AadefLlnwZ] [-gG GROUP,] [-k FIELD,] [-o FIELD,] [-p PID,] [-t TTY,] [-uU USER,]

List processes.

Which processes to show (-gGuUpPt selections may be comma separated lists):

-A  All					-a  Has terminal not session leader
-d  All but session leaders		-e  Synonym for -A
-g  In GROUPs				-G  In real GROUPs (before sgid)
-p  PIDs (--pid)			-P  Parent PIDs (--ppid)
-s  In session IDs			-t  Attached to selected TTYs
-T  Show threads also			-u  Owned by selected USERs
-U  Real USERs (before suid)

Output modifiers:

-k  Sort FIELDs (-FIELD to reverse)	-M  Measure/pad future field widths
-n  Show numeric USER and GROUP		-w  Wide output (don\'t truncate fields)

Which FIELDs to show. (-o HELP for list, default = -o PID,TTY,TIME,CMD)

-f  Full listing (-o USER:12=UID,PID,PPID,C,STIME,TTY,TIME,ARGS=CMD)
-l  Long listing (-o F,S,UID,PID,PPID,C,PRI,NI,ADDR,SZ,WCHAN,TTY,TIME,CMD)
-o  Output FIELDs instead of defaults, each with optional :size and =title
-O  Add FIELDS to defaults
-Z  Include LABEL

2. Process list running on the device

The following command prints out all the process information currently running on the device.

$ adb shell ps -ef
UID            PID  PPID C STIME TTY          TIME CMD
root             1     0 0 07:14:10 ?     00:00:02 init second_stage
root             2     0 0 07:14:10 ?     00:00:00 [kthreadd]
root             4     2 0 07:14:10 ?     00:00:00 [kworker/0:0H]
root             5     2 0 07:14:10 ?     00:00:01 [kworker/u8:0]
root             6     2 0 07:14:10 ?     00:00:00 [mm_percpu_wq]

If you use the grep command together, then you can get only the information of the package or process that you want to search for.

$ adb shell ps -ef | grep system_server
system        2033  1777 5 07:14:18 ?     00:00:32 system_server

3. Thread list running on the device

The following command prints information about all threads currently running on the device.

$ adb shell ps -T
USER           PID   TID  PPID     VSZ    RSS WCHAN            ADDR S CMD            
root             1     1     0   31784   5120 0                   0 S init
root             2     2     0       0      0 0                   0 S kthreadd
root             4     4     2       0      0 0                   0 I kworker/0:0H

If you want to get only the threads running in a particular process, you can put the PID as an option.

In this example, pid 2033 is the pid of system_server. and you can get thread list of system_server

$ adb shell ps -T 2033
USER           PID   TID  PPID     VSZ    RSS WCHAN            ADDR S CMD            
system        2033  2033  1777 2111796 193180 0                   0 S system_server
system        2033  2039  1777 2111796 193180 0                   0 S Jit thread pool
system        2033  2040  1777 2111796 193180 0                   0 S Runtime worker
system        2033  2041  1777 2111796 193180 0                   0 S Runtime worker
....

3. How to get memory usage

The command dumpsys meminfo will output a snapshot of the memory usage of the running processes.

$ adb shell dumpsys meminfo
Applications Memory Usage (in Kilobytes):
Uptime: 1474836 Realtime: 1474836

Total PSS by process:
     80,647K: system (pid 2033)
     80,004K: com.google.android.googlequicksearchbox:search (pid 2813)
     78,513K: zygote (pid 1777)
     70,264K: com.android.systemui (pid 2173)
     58,396K: com.android.chrome (pid 5356)
     55,579K: com.google.android.gms (pid 2756)
     51,626K: com.google.android.gms.persistent (pid 2579)
     47,787K: com.google.android.youtube (pid 3887)
     44,664K: com.google.android.apps.nexuslauncher (pid 2439 / activities)
     34,097K: com.google.android.apps.messaging (pid 3476)
     31,900K: com.google.android.inputmethod.latin (pid 2485)
     30,176K: com.google.android.apps.messaging:rcs (pid 5233)
....

3.1 Memory usage of a specific process

If you want to get only the memory information of a particular process, you can pass the pid or package name as an argument in the command.

  • adb shell dumpsys meminfo [pid]
  • adb shell dumpsys meminfo [package name]

In this example, this command show the memory usage of pid 2033 (system_server).

$ adb shell dumpsys meminfo 2033
Applications Memory Usage (in Kilobytes):
Uptime: 1080927 Realtime: 1080927

** MEMINFO in pid 2033 [system] **
                   Pss  Private  Private     Swap     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------
  Native Heap    21249    21184        0        0    27612    23433     4178
  Dalvik Heap    17323    17272        0        0    17135    10991     6144
 Dalvik Other     1976     1976        0        0                           
        Stack       36       36        0        0                           
...

In this example, this command show the memory usage of the package com.google.android.youtube.

$ adb shell dumpsys meminfo com.google.android.youtube
Applications Memory Usage (in Kilobytes):
Uptime: 3699899 Realtime: 3699899

** MEMINFO in pid 6789 [com.google.android.youtube] **
                   Pss  Private  Private     Swap     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------
  Native Heap    36159    36096        0        0    48536    46675     1860
  Dalvik Heap     9405     9340        0        0    13414     7286     6128
 Dalvik Other     6272     6272        0        0                           
        Stack       56       56        0        0                           

If you would like to know in detail what the output means, please refer to Android developer.

Related Posts

codechachaCopyright ©2019 codechacha