How to dump android meminfo into a file? - android

I'm taking a detailed look into the resources of my apps. Unfortunatly when I execute the adb shell dumpsys meminfo, it overflows my terminal. Anyone know how I can instead of viewing the dump in terminal, have it print into a file so I can pull and view it in a text editor?

Redirect the output - adb shell dumpsys meminfo > meminfo.txt

Redirect it using > operator:
adb shell dumpsys meminfo >meminfo.txt
Alternatively, you can pipe it directly to a viewer such as less:
adb shell dumpsys meminfo | less

In addition to redirecting, you can just pipe it through more:
adb shell dumpsys meminfo | more
Then you can just page through the output.
Or, honestly, make your terminal buffer larger so you can scroll back through it. Having a terminal where you can't even scroll back through the output of this command seems pretty ghastly. :)

use dumpsys -l and check if power option is there. Its not possible that no power state is maintained.
either you dont have write permission in the directory you are trying to create on device.
better use adb shell dumpsys power and it comes out to your pc rather than on device memory

Related

How to extract meminfo and cpuinfo of android App Programatically?

I analyze the memory and CPU utilization of my app from the terminal using the below commands
adb shell dumpsys cpuinfo
adb shell dumpsys meminfo com.company.packageName
The above approach is manual and would like to extract the data programmatically within my App. I assume android exposes SDK to fetch these values. Can you please suggest the way forward?

Getting cpu and memory usage for a period of time

I have some old shell scripts that needs to be executed on an android device but the command to fetch the total cpu, memory and swap usage is top. More specific it is:
top -m 1 -d 1.0 -n $duration
Now I have been looking to find a replacement for this and I found out that I can use dumpsys. The problem what I have is that I want to give a timeout like this:
dumpsys -t 20 cpuinfo
I checked this site: https://developer.android.com/studio/command-line/dumpsys.html but didn't find out why this doesn't work. Even when I try the help I get the same error
dumpsys --help
Can't find the service: --help
Does someone know what is going on? My current android version is 6.0.1 if this is important.
Thanks in advance!
It is true that dumpsys --help does not work. I think there is a mistake in their document. However, below works:
# adb shell dumpsys input
# adb shell dumpsys -l
Add permission on your manifest "android.permission.DUMP".or
There's another (hacky) way to access dumpsys without rooting your device - through adb shell.
This will require allowing USB debugging, and finding the port of the adb service.
Enable USB debugging on your device. This option is found under Settings -> Developer Options.
Connect your device to a PC, and run the following command from the PC's shell/command line: adb tcpip 12345. Then, from your devices shell, issue the command adb connect localhost:12345 from your application. You can now disconnect the device from USB. Alternatively, you can scan the ports on your device one by one without USB connection, using adb connect localhost: and find the port adb service is listening to.
Authorize USB debugging from the pop up confirmation dialog, if prompted. Check the "always" checkbox to do not require this step again.
Now, when you have access to the adb service, use adb shell dumpsys ... from your application code to get whatever service dump you need.

Is it possible to trigger an Android heap dump from the command line?

I would like to be able to trigger an Android heap dump from the command line. Is there a command for that?
Specifically, from the command line, not via Montior or DDMS GUIs
Maybe something like using ddms or adb, e.g. ddms -head-dump or adb shell heapdump? AFAICT monitor and ddms always start in GUI mode, and adb doesn't have a heap dump command.
Update: I tried this, it looked promising, but it doesn't work:
adb jdwp
adb forward tcp:8000 jdwp:1234 (substitute output of 1 for 1234)
jmap -dump:format=b,file=heapdump.hprof localhost:8000
But even the heap summary fails:
jmap -heap localhost:8000
Attaching to remote server localhost:8000, please wait...
Error attaching to remote server: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.net.SocketTimeoutException: Read timed out
In Android pre 3.0 you can use so called
kill -10 <pid> (more)
In Android 3.0 a new command-line tool has been added:
adb shell am dumpheap <pid> <output-file-name>; (more)
Detailed description
To get HPROF you need also change the format of it using hprof-conv
The easy way:
adb shell am dumpheap your.package.name /sdcard/dumpheap.hprof
The hard way (don't remember why I've added it originally):
adb shell am dumpheap $(ps | grep your.package.name | awk '{print $2}') /sdcard/dumpheap.hprof
If your device doesn't have awk try to use busybox awk.
After that pull the created file, convert it with hprof-conv and open it in Android Studio.

How to automate the DDMS snapshot mechanism?

Does Android DDMS provide APIs ? I need to automate the snapshot mechanism for the native heap. Or is there any post processing tool for analysis DDMS native heap snapshots.
I assume by snapshot you mean a heap dump.
From your app code you can call Debug.dumpHprofData() to dump the data.
From a script you can call
adb shell am dumpheap <process> <file>
where <process> is e.g. your process id or name, and <file> is the dump file name. After that you can adb pull the dump to your computer.
To analyze the heap dumps you can use e.g. jhat or MAT. Before that you need to run hprof-conv (included in Android SDK) on the dump to convert it from Dalvik format to standard Java format.
Further reading: Memory Analysis for Android Applications
#laalto's answer is not quite correct
From a shell you can do the following to get a heap dump for the application using adb.
Note: Replace 19000 with the process ID of your running application. The filepath must be a filepath which your application has write access to on the Android device.
Create a heap dump:
adb shell am dumpheap 19000 /sdcard/Documents/android.hprof
Pull the file to your machine:
adb pull /sdcard/Documents/android.hprof
Convert to a hprof file readable by an analyzer:
hprof-conv android.hprof mat.hprof
Tips:
Get process ID of your application:
adb shell ps | grep com.sample.application | cut -c10-15
Get process ID and dump heap:
adb shell am dumpheap `adb shell ps | grep com.sample.application | cut -c10-15` /sdcard/Documents/android.hprof
The DDMS provides a UI for the ADB. You can use ADB commands directly and process the output. The ADB documentation can be found here:
http://developer.android.com/tools/help/adb.html
I wrote small script, maybe you would find it useful
heap_dump_location='/data/local/tmp/tmp.hprof'
dump_heap() {
adb shell rm $heap_dump_location
pid=`adb shell ps | grep 'com.example.packagename' | grep -v 'packagename\.' | cut -c10-15`
adb shell am dumpheap $pid $heap_dump_location
echo "Heap dump started, we have no idea when it's done, so take a look at logs, and when is done use pull_heap_dump"
}
pull_heap_dump() {
adb pull $heap_dump_location $1
}
https://gist.github.com/logcat/8aeca0ee81af6fb0dc10bb0d58940007

Calling procrank doesn't work on real devices

according to a google io video about getting to know how much memory you app takes , you can use procrank and read the USS value of it.
i've tried it out on emulators (no matter which version i use - from 2.3.x to 4.1) and it works well , but running on an actual device , it didn't work (tested on galaxy s3 with android 4.0.4) . it's as if the command doesn't exist .
how could it be ? is there an alternative to get this USS value?
You can use dumpsys command
Steps:
issue command line: dumpsys meminfo packageName
The Private Dirty column is you wanted.
U can also use
adb shell dumpsys meminfo
or
adb shell dumpsys meminfo + pid
command
adb shell dumpsys meminfo [pid] (Private Dirty + Private Clean)
is same as
procrank (USS)
procrank and dumpsys meminfo is not the same command, because procrank can show more thread which is killed by accident.
First you shell get procrank, procmem, libpagemap.so from Google
Then do push like :
adb push procrank /system/xbin
adb push procmem /system/xbin
adb push libpagemap.so /system/lib
Last :
adb shell chmod 6755 /system/xbin/procrank
adb shell chmod 6755 /system/xbin/procmem
adb shell chmod 6755 /system/lib/libpagemap.so

Categories

Resources