2

I have listed all the installed applications. When I click any application from the list I want to show the total memory used by that app programmatically(similar to settings->apps). What I tried:

 long apsize = new File(this.getPackageManager().getApplicationInfo(
                packageName, 0).publicSourceDir).length();
        int unit = si ? 1000 : 1024;
        if (apsize < unit) return apsize + " B";
        int exp = (int) (Math.log(apsize) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
        //replacing kib and mib from the value
        return String.format("%.1f %sB", apsize / Math.pow(unit, exp), pre).replace("MiB","").replace("KiB","");

But it returns only the apk size where as I want to get the total memory used by the selected application. Is there a way to get the total memory used by application programmatically?

Arun Inbasekaran
  • 297
  • 1
  • 3
  • 14
  • Possible duplicate of [How to get current memory usage in android?](http://stackoverflow.com/questions/3170691/how-to-get-current-memory-usage-in-android) – Bishan Oct 19 '16 at 10:23

1 Answers1

0

you can get memory info programmatically and decide whether to do memory intensive work. and I think This is for current application!

Get VM Heap Size by calling:

Runtime.getRuntime().totalMemory();

Get Allocated VM Memory by calling:

Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

Get VM Heap Size Limit by calling:

Runtime.getRuntime().maxMemory()

Get Native Allocated Memory by calling:

Debug.getNativeHeapAllocatedSize();
Hamid
  • 1,493
  • 2
  • 18
  • 32