Memory leak

I work with several large drawings, and I do not know how to manage memory leaks. I tracked the heap size of my application and it does not stop growing (like allocated memory).

This is, in particular, the type "byte array (byte []), which grows and never decreases. (In the DDMS Heap view on Eclipse)

My application consists of a single action that uses fragments. These fragments display several large images. I tried to set drawables callback to null, set drawables to null, clear my volatile cache (which prevents my application from doing too much disk I / O) when I pop up a fragment, but the heap never decreases.

In fact, every time I call: Drawable.createFromResourceStream (context.getResources (), value, new FileInputStream (f), f.getName (), opts); the pile is growing. How can I free memory?

Thanks!

+7
source share
2 answers

A memory leak occurs when Java finds objects in memory referenced by your code that prevent the garbage collector from freeing this memory. A common cause in Android is a reference to an Activity context, not an application context. Make sure your context references the application (i.e. use getApplicationContext , not using this ). Watch the video for an explanation of memory leaks, and check the question .

+6
source

The answer to this question seems to be the answer, but Romain Guy's post seems appropriate for more information: Avoid memory leaks .

Apparently, if you (for example) set the text as a background image to the text view using setBackgroundDrawable * (thus attaching the selection to the view), change the orientation (destroying the action and redrawing the user interface) will still have access to old activity (after the destruction of the old activity), thereby creating a memory leak.

* (as an additional note - setBackgroundDrawable was deprecated from API level 16 )

+2
source

All Articles