The first method--recovering bitmap memory in time:
In general, the following code can be used to reclaim bitmap memory
if (bitmap! = null &&!bitmap.isrecycled ()) {
Bitmap.recycle ();
bitmap = null;
}
System.GC ();
The Bitmap.recycle () method is used to reclaim the memory occupied by the bitmap, then empty the bitmap, and finally, don't forget to call the system's garbage collector with System.GC ().
Here to declare, bitmap can have multiple (thought can have multiple if statements), but System.GC () best only one (so I wrote it out in the IF statement), because System.GC ()
The entire memory is scanned once per call, so it can affect the speed of the program if it is called multiple times. For the sake of the efficiency of the program, I put it behind all the recycling statements,
This has played its effect, but also save time.
Recycling bitmap already know, then "timely" how to understand it?
According to my practical experience, the place where bitmap works is either in view or in activity (certainly there are other areas, but the principle is similar),
The place to recycle bitmap is best written in the moments when these areas just don't use bitmap.
For example, if the view uses bitmap, it should be recycled when the view is no longer drawn, or in the code of the next area that jumps to it;
Again such as say Surfaceview, should in onsurfacedestroyed this method of recovery;
Similarly, if the activity uses bitmap, it can be recycled in the OnStop or OnDestroy method ...
In combination with the above common denominator, the principle of "timely recovery" is to be recycled at the end of an area where bitmap is used or at the end.
The second method-Compress the image:
This method is of course very simple, that is, to make the picture size smaller,
There are two ways of doing this:
One is to reduce the quality of the picture (the resolution is constant),
The other is to reduce the image resolution (resolution Change).
In short, make the picture size smaller on the line.
Practice has shown that reducing the quality of the picture (resolution) can greatly reduce the volume, and the difference in quality is not obvious to the naked eye.
I just started to use these two methods, the principle is very simple, but my bug occurred although not so frequently, but it still exists!!
After a few days of hard work and try, combined with some of the specifics of my project, I finally solved this headache bug, but the fact is a bit unexpected.
When I use the above two methods of the bug is still not resolved, I began to suspect that bitmap more than 8M will be an error, but now I have the back and forth bitmap are recycled,
There is no more than 8 m, so why do you still get an error?
Finally I found this reason: When the memory has been used by some bitmap, whether it is recycled or not, it will become particularly "sensitive", this time,
If the bitmap suddenly take up a lot of memory, even with the previous memory added up to less than 8 m, the system will also error, because it becomes "sensitive"!
I don't know how this is better explained by the underlying principle, but I think the word "sensitive" should be very visually interpreted.
So, in order to conform to the "sensitivity" of memory, I would need to load a number of large volume bitmap at the same place to modify, using the following methods:
Compression to save bitmap memory space-key steps to fix a bug
Bitmapfactory.options opts = new Bitmapfactory.options ();
Opts.insamplesize = 2; This value is compressed in multiples (2 integers), the smaller the value, the smaller the compression rate, the clearer the picture
Returns the Bitmap object after the original image was decoded
Bitmap = Bitmapfactory.decoderesource (Context, Resourcesid, opts);
That is, the picture is reduced by one times, and then the picture is reduced by one times as bitmap into memory, so that it occupies bitmap memory greatly reduced.
Later, after testing, the bug was solved. The picture shrinks once, conforms to the memory "the sensitivity", also will not have the error.
The above method should be sufficient to solve the most bitmap memory overflow problem, but the specific situation is to be analyzed.
Android Bitmap Recycling