Memory and Activities :: OutOfMemory

前端 未结 3 2152
一生所求
一生所求 2020-12-19 11:35

SETUP :

I have this app which has 4 activities in a linear path, really simple navigation : A -> B -> C -> D

All the activities share the

3条回答
  •  眼角桃花
    2020-12-19 11:53

    Memory is a very tricky subject in Android.

    Every app gets a heap memory limit depending on the device. This heap memory is the dalvik memory plus the native memory, and you can see it as the total column in the dumpsys meminfo results. The dalvik memory deals with everything except with the bitmaps, that are allocated in the native memory (this is true for Android versions before Honeycomb).

    Having said that I can only answer to some of your questions:

    1. As far as I know, Android will always allocate memory for Bitmaps, even if they are the same. Therefore, in your case, every activity allocates memory for your background.

    2. I don't know if its better to work with themes, you'll have to try that.

    3. On one hand, activities are not reclaimed while the device has enough memory to deal with the next activity. Every activity is pushed to a pile from where it is recovered when you pressed the back button. In case Android needs more memory it removes one activity from the pile deallocating its memory (going back to question number one, maybe this is the reason for not sharing memory). On the other hand, you can set the activities launchMode to change this behaviour (have a look here).

    4. I think MAT doesn't show native memory data. Use dumpsys meminfo's native column to see how much allocated memory for Bitmaps you have.

    I have had hard times dealing with OutOfMemory problems myself. Now I have a much more clear idea of how it works and I am able to work with large files without running out of memory. I would highly recommend these two resources that helped me a lot:

    • How to discover memory usage of my application in Android
    • Google I/O 2011: Memory management for Android Apps (extremely useful!)

    Good luck!

提交回复
热议问题