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.

5

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I use Nexus S and it have 320MB of RAM, where I can see that ?
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.
but if my phone have 320MB ram why it use form 16 to 48 ??
Also there is an Google I/O talk about memory management in Android: link. It is explained there.
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.
|
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]));

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.