Although android has its own garbage collection mechanism, it depends on the situation if we want to call Recycle by ourselves. It is assumed that only a few images are used. However, if there are a large number of bitmaps that require garbage collection, the number of times that need to be done for garbage collection will also occur more frequently, which will load system resources. Therefore, it is better to try to release the Recycle by yourself.
It is worth considering how to use the recycle () method and when to try recycle. No errors:
05-15 08:31:00.117: E/AndroidRuntime(513): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap [email protected]
Java execution exception: Try to use an image that has been recycled
Only when you confirm that you are not using this bitmap, you can choose to call the recycle () method to release it.
The recycle () call encountered an exception when I was trying a small notebook today. The reason is:
In the XML file, imageview has two methods to set a display image:
android:background="@drawable/aa" android:src="@drawable/aa"
At the beginning, I used background and set it in the program.
// Obtain the currently displayed image bitmap bitmap1 = (bitmapdrawable) imageview. getbackground ()). getbitmap (); bitmap bitmap2 = bitmap. createbitmap (bitmap1, 0, 0, bitmap1.getwidth (), bitmap1.getheight (), matrix, true); // if the image has not been recycled, forcibly recycle if (! Bitmap1.isrecycled () {bitmap1.recycle ();} // create an image imageview. setimagebitmap (bitmap2) based on the original bitmap and matrix );
The imageview. setimagebitmap () method sets the SRC image instead of the background. However, I recycled the background image. In this way, an exception occurs.
So change it to the following code to call the setbackground () method.
// Obtain the currently displayed image bitmap bitmap1 = (bitmapdrawable) imageview. getbackground ()). getbitmap (); bitmap bitmap2 = bitmap. createbitmap (bitmap1, 0, 0, bitmap1.getwidth (), bitmap1.getheight (), matrix, true); // if the image has not been recycled, forcibly recycle if (! Bitmap1.isrecycled () {bitmap1.recycle ();} // create a new image based on the original bitmap and matrix // imageview. setimagebitmap (bitmap2); imageview. setbackgrounddrawable (New bitmapdrawable (bitmap2 ));
Therefore, you must pay attention to the source of the imageview image and then perform the corresponding recycle.
I. Recycle of Bitmap