Android, I see heap growing, but I want it to stop

2019-03-31 05:59发布

I see my heap growing and I know it will eventually crash on any device since it just keeps growing.

Grow heap (frag case)

is seen throughout the logs

on my phone it will crash the app at 32mb used. Other phones will of course be 16mb, if there are any with that few resources that run android 2.2

Now, I am using recycle() on my bitmaps, setting things to null, popping items from complex data structures, and using System.gc() to invoke garbage collection, throughout the app

but, the heap still grows and its a problem... eventually

How do I just force the app to dump resources so that it can continue functioning.

it is usually always the "bitmap vm budget" exceeding, but I am feeling more and more that I just don't have access to the "clear bitmap vm" command

1条回答
Root(大扎)
2楼-- · 2019-03-31 06:31

i had also struggled a lot with this issue. there are many a things you can do.

  1. you can call recycle on each bitmap and set them to null.(bitmap.recycle() with relaese all the memory used by this bitmap but does not nullify the bitmap object ).

  2. you can also unbind the drawables associated with layouts as specified in this link.

  3. you can convert your hashmaps to WeakHashmaps. so that its memory would get relaesed when system runs low on memory.

  4. you can resize all your bitmaps. have a look at this link..

  5. you can override onLowMemory() method in activity which gets a call when entire system runs low on memory. you can release few resources there.

  6. you can make your objects SoftReference or Weakreference, so that they get released in low memory condition.

But the real fact is that, all this can DELAY your out of memory issue/crash, but can not eliminate it because thing is that you must be leaking your activity context or memory somewhere.

查看更多
登录 后发表回答