Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a Bitmap resource in a static variable

Tags:

java

android

I have a view which displays a small bitmap, and this is used in many places in my application (especially list views). I'm currently loading this bitmap each time an instance of that view is created using BitmapFactory.decodeResource(resource, id). I realized that I can improve performance by loading that bitmap once into a static variable (so that all instances of the view reuse it) and that did indeed save about 2-4 ms per view instance. My question, does using a static variable in this way cause any type of memory leak in Android? I'm worried because I haven't found any other example that uses a static variable to store a bitmap like this.

Follow up question: How to keep a Bitmap in memory

like image 977
Abdullah Jibaly Avatar asked Nov 14 '22 11:11

Abdullah Jibaly


1 Answers

Keeping bitmap objects as static may cause potential memory leaks, see official documents at http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

like image 162
CalvinLee Avatar answered Nov 16 '22 04:11

CalvinLee