Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get free process memory on android

How to get the amount of free memory of current running process on android? Or how to get the total amount of memory allocated to current process programmatically?

Method ActivityManager.getMemoryInfo() doesn't work in my case because it returns free SYSTEM memory, but not the process memory. There's also ActivityManager.getProcessInfo() method but it returns an old API struct and I don't know whether it's possible to retrieve amount of free memory using that data.

Any explanations on that methods or maybe some other ways to get the amount of free memory?

like image 289
Yury Pogrebnyak Avatar asked Apr 01 '26 07:04

Yury Pogrebnyak


2 Answers

You can also use Runtime.getRuntime().maxMemory(); which returns the maximum heap size for your process in bytes.

Althoug this is usually the same as memory class, I've found situations when memory class returns a higher value (i.e. 24MB) and heap size returns a lower value (i.e. 16MB), and the value realy available for the processs is the lower one.

like image 153
Luis Avatar answered Apr 02 '26 22:04

Luis


try the following code:

      ActivityManager activityManager = = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
      MemoryInfo mi = new MemoryInfo();
      activityManager.getMemoryInfo(mi);
      Log.i("memory free", "" + mi.availMem);
like image 21
G M Ramesh Avatar answered Apr 02 '26 20:04

G M Ramesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!