What does Debug.getNativeHeapFreeSize really mean
Asked Answered
G

0

7

I use the following code to dump memory usage in my app:

public static void dumpMemoryUsage(Context ctx) {
    try {
        MemoryInfo mi = new MemoryInfo();
        double allocated = new Double(Debug.getNativeHeapAllocatedSize());
        double available = new Double(Debug.getNativeHeapSize());
        double free = new Double(Debug.getNativeHeapFreeSize());
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        long availableMegs = mi.availMem / 1048576L; //1MB
        long threshold = mi.threshold / 1048576L; //1MB
        String descr = String
                .format(Locale.US,
                        "+++System available MB:%d\nIs low:%b\nThreshold:%d\nMem class:%d\nallocated:%f\nfree:%f\navailable:%f\n",
                        availableMegs, mi.lowMemory, threshold,
                        activityManager.getMemoryClass(), allocated, free, available);
        L.l(descr);
    } catch (Exception e) {
        L.l(e);
    }
}

And I run it on two different devices Nexus (2011 model) and Galaxy Note 1. Galaxy Note has memory issues and fails very often with memory problems as soon as I add 1-2 bitmaps.

Here is the dump:
Nexus:

+++System available MB:154
Is low:false
Threshold:94
Mem class:96
allocated:5663544.000000
free:1377480.000000
available:7081984.000000

Galaxy Note:

+++System available MB:316
Is low:false
Threshold:64
Mem class:64
allocated:5361664.000000
free:155648.000000
available:5832704.000000

Note that the Debug.getNativeHeapFreeSize() call returns 1377480.000000 on the nexus and ONLY 155648.000000 on the galaxy. How is that possible. What does this call really mean? I did both tests on a fresh app install with no work done by the app - just presenting the first screen which barely has a a few TextViews and buttons. Galaxy Note 1 has memory class of 64MB so I don't think I am hitting that limit. I have no memory problems on the Nexus even when I add many bitmaps on the screen so I don't think I am using too much memory.

Girl answered 12/10, 2013 at 9:41 Comment(1)
It returns the amount of free memory in the native heap - in bytes. You can look at the source here: grepcode.com/file/repository.grepcode.com/java/ext/…Lara

© 2022 - 2024 — McMap. All rights reserved.