goddice/droidlife

Potential Memory Leak

Opened this issue · 0 comments

I'm a CS grad student working on a static analysis tool to detect memory leaks 
in Android. My tool found a potential memory leak in this app based on the 
static seederManager field of the SeederManager class. The problem is that if 
SeederManager.getInstance(Context context) is called with an instance of 
Activity bound to context (which happens in several places, such as in 
DesignActivity and SeedersActivity), the static seederManager field will retain 
a reference to to the Activity and prevent it from being garbage collected 
after its destruction, at least until seederManager is overwritten. It looks 
like this should usually happen pretty quickly, so the leak should be very 
benign in most if not all cases.

I think two potential fixes are to change
seederManager = new SeederManager(context); 
to 
seederManager = new SeederManager(context.getApplicationContext());.
which would prevent the seederManager field from retaining a reference to 
Activity context, or to not use the singleton pattern in SeederManager (i.e., 
delete the seederManager field and expose a public constructor), which would 
guarantee that the lifetime of the SeederManager instance is the same as the 
lifetime of the Activity constructing it. I don't understand the high-level 
structure of your app enough to know which of these fixes (if either) is more 
appropriate.

Background:
My static analysis tool looks at each static field in the program and 
determines if there is some instance of an Activity that is reachable from a 
static field. If it thinks this might be the case, it explores all possible 
program executions that would produce the heap path from the static field to 
the Activity instance in an attempt to confirm the leak. The reason it checks 
this property is that if the Activity is destroyed, but a static field keeps a 
reference to the Activity, the garbage collector can't pick it up, leading to a 
memory leak.

What version of the product are you using? 
I'm using the source code for version 3.2 of the Android library (Gingerbread)

Original issue reported on code.google.com by sam.blac...@gmail.com on 8 Nov 2012 at 7:17