Reputation: 10755
I want to gather some statistics from my Android phone.
When I start my application I want to know following:
What I must do to gather that statistics, maybe write some code, or maybe you use some applications which give such information or maybe something else. I use MAT but it is not helpful for me maybe I miss something.
The main reason why I want to gather such statistics is that may application start to kill other applications in order to free more space and if I can gather such information I will understand everything.
For Example my application use library (helper.jar) which size is 85 MB, I know that if application use .jar it loads to the RAM of the phone, now how I can see that my RAM grows from 2 to 2 + 85 (size of .jar).
If I type adb shell cat /proc/meminfo this command from ADB I will see some information about memory.
How I can do same from JAVA code ?
Best Regards, ViTo Brothers.
Upvotes: 5
Views: 4199
Reputation: 258
try this.it will give u memory info related your apps and total memory as u needed.
private void getProcessInfo(){ PackageManager pm = this.getPackageManager();
MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
Map<Integer, String> pidMap = new TreeMap<Integer, String>();
for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)
{
pidMap.put(runningAppProcessInfo.pid, runningAppProcessInfo.processName);
}
Collection<Integer> keys = pidMap.keySet();
for(int key : keys)
{
ModelProcessInfo item=new ModelProcessInfo();
int pids[] = new int[1];
pids[0] = key;
android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray)
{
Log.i(TAG, String.format("** MEMINFO in pid %d [%s] **\n",pids[0],pidMap.get(pids[0])));
Log.i(TAG, " pidMemoryInfo.getTotalPrivateDirty(%d): " + pidMemoryInfo.getTotalPrivateDirty() + "\n");
Log.i(TAG, " pidMemoryInfo.getTotalPss(): " + pidMemoryInfo.getTotalPss() + "\n");
Log.i(TAG, " pidMemoryInfo.getTotalSharedDirty(): " + pidMemoryInfo.getTotalSharedDirty() + "\n");
//pss->RAM used by your process
}
// activityManager.killBackgroundProcesses(pidMap.get(pids[0]));
}
}
Upvotes: 2
Reputation: 1632
If you want to know how much RAM your application can use looks at ActivityManager.getMemoryClass.
There is this limitation on the memory an application can have so that an application cannot steal all memory from other applications. Basically your app shouldn't be able to kill other applications. At least as long as you don't use the option "large heap size" in your app.
The limitation per application is like 16 (on G1) to 48 MB. It depends heavily on the total RAM of your device how much your memory your app can have. The 48 MB for example apply for 1GB RAM.
A method for getting the current memory usage of your app is to look at logcat. It will say something like
GC_CONCURRENT freed 1109K, 12% free 10115K/11463K, paused 5ms+2ms
meaning your program takes uproughly 11 MB of RAM.
Upvotes: 2