Android bitmaps, heap recycling and fragmentation

I have an application that heavily uses bitmaps read from an SD card. I am 99% sure that my application will never be destroyed without reusing them. they are all allocated to the LRU cache (collective collections), which process them, because they are cleared from the LRU cache, and I process the rest in onDestroy ().

However, I still have problems. if I start / stop / start / ... my application several times, I always get a memory error.

my theory about this is that it should make w / android a non-compacting bunch. after several runs, the heap is quite fragmented, therefore, although there is a lot of free memory, there are no adjacent pieces large enough to load bitmap images. what a theory anyway.

I "solved" this by killing my process in onDestroy () (android.os.Process.killProcess ()). this solves the problem, but introduces other own problems, since I have other threads and services that need to be executed, and waiting for them all is not so simple. not to mention the fact that I, of course, understand that this is just a very bad practice.

I read all posts, etc. on this topic, and I do not see a good solution. I am using Thumbnails.getThumbnail (), so I am already using smaller versions of the images. their subsampling also leads to very poor image quality.

I store a cache of 8 miniature thumbnails and 36 β€œmicro” miniatures, so I never had more than in memory at a time. they should all fit in a few megabytes at the most.

Any ideas / tips / pointers are welcome. I am at my end of the mind on this.

+4
source share
1 answer

You say that you recycle them in onDestroy() , but maybe you should recycle them in onPause() ? When switching the orientation, I ran into some memory problems, and the problem was solved by recycling in onPause() .

Just a thought. Not sure if this will solve your problem.

+2
source

Source: https://habr.com/ru/post/911262/


All Articles