3

I want my app heap memory size check.

fist I use android:largeHeap="true"

how to my app heap memory size check on android?

Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89
hyunwooks
  • 131
  • 1
  • 12
  • Probably duplicate question. see here https://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android – Raheel Aug 02 '17 at 08:21
  • Unless you have a very unique app, you shouldn't be setting largeHeap=true. If you think you need it, you should probably look at how your app behaves and use less memory. – Gabe Sechan Aug 02 '17 at 08:32

1 Answers1

0

The amount of memory depends on the Android version and the device model.

public static void logHeap() {
        Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1024*1024));
        Double available = new Double(Debug.getNativeHeapSize())/(1024*1024.0);
        Double free = new Double(Debug.getNativeHeapFreeSize())/(1024*1024.0);
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);

        Log.d("tag", "debug. ===Heap====Heap======Heap====Heap====Heap=====");
        Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)");
        Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/(1024*1024.0))) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/(1024*1024.0)))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/(1024*1024.0))) +"MB free)");
    }
Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89