Badal Badal - 4 months ago 27
Android Question

How to get current memory usage in android?

I have used /proc/meminfo and parsed command response.however it result shows that :

MemTotal: 94348 kB
MemFree: 5784 kB

means. it shows there is only 5MB free memory. Is it possible with android mobile?
There is only 5-6 application installed on my mobile and no other task is running. but still this command shows there is very little free memory.

Can somebody clarify this? or is there any other way of getting memory usage in android?

Answer

Thank you. Its done and it works !

Let me tell you what I did, So others who visit this thread can come to know the steps:

  1. parse /proc/meminfo command. You can find reference code here: Get Memory Usage in Android

  2. use below code and get current RAM:

    MemoryInfo mi = new MemoryInfo();
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    double availableMegs = mi.availMem / 1048576L;
    
    //Percentage can be calculated for API 16+
    double percentAvail = mi.availMem / mi.totalMem;
    

Explanation of the number 1048576

1024 bytes      == 1 kilobyte  
1024 kilobytes  == 1 megabyte  

1024 * 1024     == 1048576

It's quite obvious that the number is used to convert from bytes to megabytes

P.S: we need to calculate total memory only once. so call point 1 only once in your code and then after, you can call code of point 2 repetitively.

Comments