How to clear bitmap resources

I wanted to ask this question, which was asked several times earlier. Sorry if I repeat again, but I donโ€™t understand that this is the best solution here.

Question: "How to clear the bitmap ImageView resource after using it so that we do not have references to it in memory?".

Here is an example:

  • Screen 1 redirects to Screen 2
  • Screen 2 contains the A control (large photo preview)
  • Control A contains ImageView B
  • ImageView B installs when control A starts

Everything works great for the first time. As soon as the A control is completed, I am redirected from screen 2 to screen 1. At this point, all the links for the A or ImageView B or Bitmap control must be dead. THEY ARE NOT!!!

I tried all kinds of solutions, including bitmap.recycle (), adding finalize () to Control A, System.gs () and nullifying the control in different places like onStop () and onDestroy (), and everything else on screen 2 nothing works!

The problem is that Iโ€™m viewing the screen again a second time, so Iโ€™m moving from screen 1 to screen 2 (i.e., again creating a preview of the photo). I am getting a memory exception. As far as I understand, the link to the previous bitmap is not cleared.

How can I kill him before redirecting back to screen 1?

One thing I noticed. If I reduce the size of the photo, say, crop or reduce the size of the image, everything will go smoothly, several times ... before I get the same problem. So basically it will take a little longer to fill out.

I would really appreciate a solution to this problem, as it is important.

+4
source share
2 answers

You probably have a memory leak, this video can help you find the problem with Google I / O 2011: memory management for Android apps .

Note: pre 3.1 bitmaps are stored in the heap memory of the VM, but in their own memory, which causes a lot of problems when detecting leaks, for more information see the video

+2
source

See how WeakReference is used.

0
source

All Articles