Search code examples
javaandroidandroid-memory

Runime.getRuntime().totalMemory() and ActivityManager.MemoryInfo()


Why total memory extracted from getRuntime().totalMemory() is not equal with when we use ActivityManager.MemoryInfo()? In below two code part I get different values:

long totalMemory = Runtime.getRuntime().totalMemory() ;

and

ActivityManager actManager = (ActivityManager) getActivity().getSystemService(ACTIVITY_SERVICE);
                ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
                actManager.getMemoryInfo(memInfo);
                long totalMemory = memInfo.totalMem

in first code I get 12.759.040 and from the second code I get 907.034.624 !


Solution

  • Runtime.getRuntime().totalMemory() is

    Returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment.

    memInfo.totalMem is

    The total memory accessible by the kernel. This is basically the RAM size of the device, not including below-kernel fixed allocations like DMA buffers, RAM for the baseband CPU, etc.

    Note that the amount of memory required to hold an object of any given type may be implementation-dependent.

    • The first one is the memory the jvm running your process holds, about 12 MB.
    • The second one is the total kernel accessible system memory, about one gigabyte in your case.

    The first is part of the second.