How can I gracefully degrade my performance, given limited memory?

后端 未结 1 897
深忆病人
深忆病人 2021-01-19 14:39

I\'ve spent the last few days trying to remove memory leaks in my game, resulting in many out of memory errors. I\'m on the verge of adding a significant amount of graphics,

相关标签:
1条回答
  • 2021-01-19 15:17

    Without seeing your actual code, I can't say if the following will be relevant to you or not. However, it is worth a shot.

    If you are not already doing so, you can consider using something called an LruCache. http://developer.android.com/reference/android/util/LruCache.html

    Using this tool, you can determine at what point your cached objects (such as Bitmaps) will become eligible for garbage collection. So, if you want to set it at 4mb (for example) the OS will deal with it should it try to grow beyond it. (See docs for implementation details and a good example).

    The only downside is that that little gem only came along with 3.2, so you would have to make that your min SDK in the AndroidManifest, or do a check programatically at run time for the api level to determine if you can use it. Pre 3.2 I would say you need to call recycle() on any Bitmaps you are using, but if you have optimized already I would think the chances are good you are already doing this.

    This is a nice little snippet about the difference between the heap and native memory. http://code-gotcha.blogspot.com/2011/09/android-bitmap-heap.html It may (depending on what you are doing) help you to understand why you are not seeing the drop in memory you are expecting.

    And finally this post from SO should help when dealing with heap size as well: Detect application heap size in Android

    Hope that helps.

    0 讨论(0)
提交回复
热议问题