Need to vent on the bane of all my Android code. Apart from one release that contained an unfortunate bug that I didn’t catch, at least 80% of all the force closes I see are caused by the above error; the remaining force closes being almost always caused by code that has been implemented to prevent the above error.
Has anyone got some good tips on how to avoid this? Google’s official stance seems to be “use smaller bitmaps” as well as denial of claims that Android leaks resources; in other words, no help at all. I know exactly the amount of graphics that should be loaded in memory and - it’s large - but nowhere near 16mb. I can note down some of the steps that I’ve used that have reduced the problem a lot - though not eliminated it entirely.
- Large bitmaps loaded using Bitmap.Config.RGB_565 ( = smaller size).
- All the bitmaps loaded up front in a centralized ImageLibrary class. This ensures that no large bitmaps are loaded more than once, and that all the bitmaps are explicitly recycled and nulled when the application switches out.
- In general, the code is gone through with a fine tooth comb to prevent any form of memory leaks. Difficult to guarantee none, of course, but there is definitely no large scale mem leaks left.
- Explicit call of System.gc() when cleaning out/redrawing large bitmaps. Ugly and slow, but every little thing helps.
- Bitmapfactory.decodeResources (which has a reputation for memory leaks) is cleaned out of the code. Removing those calls seemed to reduce out of memory errors a good deal (though perhaps I am just imagining things). I am contemplating removing bitmaps out of the layouts as well now, as that is presumably the only place left where Bitmapfactory is being used.
Anyone knows of any tricks I’ve missed?