Bitmap calls recycle () to reclaim memory

Source: Internet
Author: User

Bitmap calls recycle? When?

 

 

Bitmap has a recycle method, which is very simple. It recycles the space of Bitmap.

 

Q 1: Is bitmap necessary to call the recycle method?

A: embedded systems always pay special attention to space issues. If you are not careful, oom will occur. However, the Android platform using Java on the application layer has its own advantages [Java language has its own garbage collection, and each application on the Android platform has its own process space ].

The reasons for not calling bitmap are as follows:

A. Garbage collection will be handled;

B. When the application is closed and the process is killed, all the space occupied by the process will be restored to the system;

 

However, if you are a little clean, idealistic, or controllable, or idle... The recycle function of Bitmap can still be called for the following reasons:

A. Garbage collection is good, but if possible, let's keep it idle. Garbage collection has a lot of uncertainty in the future, which will increase the loading of unknown time points in the future. If a large number of bitmaps need garbage collection, therefore, the number of times that garbage collection needs to be performed is more frequent, which may result in ANR. However, if you recycle yourself, you can control the scattered processing of these recycling tasks.

B. If the application is always running like launcher, its process will always exist, and memory problems may be better.

 

Q2: When?

A: timing is very important here. If it is too early, it will lead to an exception like this:

Java. Lang. runtimeexception, canvas: trying to use a recycled bitmap

Android. Graphics. Bitmap @ 44ebeee0, canvas. Java, 955

So, how can we ensure it is not too early?

Important time points for image display:

Step 1: set the time point;

Step 2: the time point of the image;

The safest and most stupid way is to recycle the old image after the new image is set. The disadvantage is that, in a certain period of time, the space you need is double [both the new and old sets are in ];

If you are not inclined to do so and have time, you can consider the next time point. Besides the setimage and other code, we will check the bitmap when calling the bitmap, when acticvity becomes visible, the system will still find the previously configured bitmap. [even if refresh UI is not mentioned in your onresume method, it will also do this, otherwise, it probably won't know what to display this time ]. Therefore, in the UI thread, in a method that cannot be interrupted, it is not affected whether to set a new bitmap or recycle the old image first.

For example, mbitmap. Recycle ();

Mbitmap =... // set

Mimageview. setimage (mbitmap );

Such code is completely feasible.

 

The most important thing to follow is to make sure that the UI thread is in a method that cannot be interrupted [because UI display can only be set in the UI main thread. This is to ensure that the main UI thread cannot be interrupted between the two, and it cannot change from invisible to visible.

Therefore, two things are very important:

1. multithreading [I personally think it is best not to call the recycle method of Bitmap used by the UI in other threads. It is difficult to ensure the time sequence between multithreading, I have not come up with a reasonable method for recycle in the background thread ];

2. non-timely method: for example, sending intent, sending notify y to notify the main UI thread to refresh the UI and cannot replace mimageview. setimage (mbitmap. It is entirely possible that you did send intent, but one of the target activities has not re-set the UI. [Q: Maybe did not receive or receive it, but it is still waiting for processing, not sure whether the two possibilities are possible.] at this time, the acitivity becomes visible, and the system still tries to find the old image. If it cannot be found, an exception will be reported.

 

PS: Java. lang. runtimeexception, canvas: trying to use a recycled bitmap android. graphics. bitmap @ 44ebeee0, canvas. you may not be able to see exceptions such as Java and 955. The default log only seems to be able to see uncaught exception. The first view is the events of monkey. log, if you know how to open the corresponding mobile phone log Trace, you can also see.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.