How to extract meminfo and cpuinfo of android App Programatically? - android

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?

Related

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.

adb shell command to find the device RAM

I am trying to find device RAM (512 MB or 1GB) using adb shell commands.
Following commands giving more details about the free,used & total memory. But how to find the device overall RAM?
adb shell "cat /proc/meminfo"
adb shell dumpsys meminfo
adb shell procrank
So it looks to me that MemTotal is probably the field you are looking for:
MemTotal — Total amount of physical RAM, in kilobytes.
While it is not the strictly Android, another Linux flavour CentOS provides the following page regarding /proc/meminfo. It seems that Red Hat, and other variants also describe it similarily.
Is there something that is making you suspect that this is not the physical RAM? On my device the value reported for MemTotal matches what I expect.
People who post answers often forget that Windows users don't have access to grep, cat, etc. Add shell to the beginning of your pipe.
.\adb.exe -s whichever-device shell "cat /proc/meminfo | grep MemTotal"
This is assuming you're connected to multiple devices. If you're only connected to one, you can remove -s whichever-device (usually the ip if you're connected by wifi or the device number from the device list if connected by wire.).

Cpu usage information through ADB

I am trying to measure the cpu usage of an Android application through adb. I am approaching this by doing the following commands:
dumpsys cpuinfo packagename
adb shell top | grep packagename
I get different CPU usage stats from each one of these commands. What exactly is the difference between them and which would be more reliable if I am looking to create a graph of the cpu utilization of the application wrt the input provided.

How to dump android meminfo into a file?

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

how to get the heap size value?

How can I get the heap memory sizes of different processes running on emulator (or device) to my application (Activity) in Android?
Open a command prompt, type
adb shell dumpsys meminfo
or for jusr one process, either
adb shell dumpsys meminfo xxx
where xxx is the PID
or
adb shell dumpsys meminfo 'your apps package name'

Categories

Resources