5

I want to gather some statistics from my Android phone.

When I start my application I want to know following:

  • How much RAM does application use.
  • How much free RAM does my phone have.
  • How much total RAM my phone have.

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.


Edited

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.

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • possible duplicate of [How to discover memory usage of my application in Android](http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android) – KV Prajapati Dec 13 '11 at 10:55
  • @AVD I have read the post that you have selected as duplicated and I can't find there answer on my question ! – Viktor Apoyan Dec 13 '11 at 10:58
  • I have read many articles and search throw internet to find answer, but I still can't find. So if you can pleas help ! – Viktor Apoyan Dec 13 '11 at 10:59
  • Please take a look at SO thread and a blog - http://stackoverflow.com/questions/3170691/how-to-get-current-memory-usage-in-android , http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html – KV Prajapati Dec 13 '11 at 11:03
  • @AVD I want to know not a heap size, I want to know how many RAM does my application use. – Viktor Apoyan Dec 13 '11 at 11:38

2 Answers2

2

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]));

    }

}
neeraj kirola
  • 258
  • 2
  • 13
2

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.

js-
  • 1,632
  • 1
  • 14
  • 18
  • I use Nexus S and it have 320MB of RAM, where I can see that ? – Viktor Apoyan Dec 13 '11 at 11:04
  • As I said: Use getMemoryClass(). It returns an integer that states how many MB your application can have. So it will return a value between 16 and 48. – js- Dec 13 '11 at 11:06
  • but if my phone have 320MB ram why it use form 16 to 48 ?? – Viktor Apoyan Dec 13 '11 at 11:07
  • Also there is an Google I/O talk about memory management in Android: [link](http://www.youtube.com/watch?v=_CruQY55HOk). It is explained there. – js- Dec 13 '11 at 11:08
  • 1
    It can only use so little RAM so that Android and other applications do not run out of RAM because your application consumes all of it. – js- Dec 13 '11 at 11:09
  • I have watched Google I/O talk about memory management, but it give me nothing, I want just know how many free RAM do I have in my phone – Viktor Apoyan Dec 13 '11 at 11:09
  • For example I have .jar which is 100 MB, Then I load that .jar to RAM how I can see how my RAM value changed ? – Viktor Apoyan Dec 13 '11 at 11:10