简体   繁体   中英

How to clean up bitmap resources

I wanted to ask this question that was asked a few times before. Sorry if I am re-itterating but it is not clear to me as to what is the best solution here.

The question is "how to clean ImageView bitmap resource after its been used so we don't have references to it in memory?".

Here is an example:

  1. Screen 1 redirects to Screen 2
  2. Screen 2 contains control A (preview of large photo)
  3. Control A contains ImageView B
  4. ImageView B is set when control A is initiated

Everything works fine the first time around. Once control A is done I redirect from Screen 2 to Screen 1. At this point all references of control A or ImageView B or Bitmap that its using should be dead. THEY ARE NOT!!!

I've tried all kinds of solutions including bitmap.recycle(), adding finalize() into Control A, System.gs() and nulling control in variouse places such as onStop() and onDestroy(), and everything else that's on screen 2, nothing works!

The problem is when I revisit the screen second time around so going from screen 1 to screen 2 (ie creating preview of photo again) I get out of memory exception. It is my understanding that the reference of the previous bitmap is not cleaned up.

How do I KILL it just before I redirect back to Screen 1?

One thing I noticed. If I reduce the size of the photo by, say cropping or making a smaller size of the image everything goes smooth, few times... before I get same issue. So basically it just takes a bit longer to fill up.

I would really appreciate some solution here as this is critical.

You probably have a memory leak, this video might help you in finding the problem Google I/O 2011: Memory management for Android Apps .

Note: pre 3.1 bitmaps are store in VM heap memory but in native memory, which causes lot of problems in noticing leaks,for further info refer the video

Have a look at how WeakReference is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM