How do I discover memory usage of my application in Android?

前端 未结 9 2343
清酒与你
清酒与你 2020-11-21 06:27

How can I find the memory used on my Android application, programmatically?

I hope there is a way to do it. Plus, how do I get the free memory of the phone too?

9条回答
  •  無奈伤痛
    2020-11-21 06:44

    Yes, you can get memory info programmatically and decide whether to do memory intensive work.

    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();
    

    I made an app to figure out the OutOfMemoryError behavior and monitor memory usage.

    https://play.google.com/store/apps/details?id=net.coocood.oomresearch

    You can get the source code at https://github.com/coocood/oom-research

提交回复
热议问题