A lot of times, we don't have much problem using bitmap. However, when the machine resources are not enough, many applications will be reported OutOfMemoryError, of course, reported such an exception is often given to bitmap memory when memory is not enough. Even a small picture, there will be errors, how to solve it?
My solution is as follows:
[Java] view plaincopy
-
Public StaticBitmap CreateBitmap (intwidth,intheight, bitmap.config Config) {
-
Bitmap Bitmap = null;
-
Try {
-
Bitmap = Bitmap.createbitmap (width, height, config);
-
} catch
-
while (bitmap = = null) {
-
System.GC ();
-
System.runfinalization ();
-
Bitmap = CreateBitmap (width, height, config);
-
}
-
}
-
}
Recent experiments have shown that when memory leaks are large, the above approach simply does not prevent memory overflow. In fact, to avoid memory overflow or to rely on the usual writing code when you pay attention to. Many times the overflow is mainly due to the bitmap and drawable resources are not released, especially when the project is a lot of picture resources, this problem is particularly prominent.
Workaround: After CreateBitmap, when this bitmap is exhausted, it needs to recycle this resource.
There is also the need to change the background resources of the picture need to use to drawable, when the need to replace the drawable, the original drawable need setcallback (NULL) to enable the activity to dereference the original resource, so that the resources are freed. Java memory management is not like C + + general, but there is its flexibility, as long as you think about it, you can manage the memory well.
If you have a better solution, you can discuss it together.
Android Some thoughts about OutOfMemoryError