java - Understanding Memory Leaks in an Android application -
i'm rather new java programming, c++ experience, , i'm reading how references can lead memory leaks in android applications. this explanation has me stumped. @ "lesson #2" says:
the point is, activity doesn’t know lifespan of someobject end when instance of activity ends. if object remains in memory, hold activity in memory [...].
as see (probably wrong; please correct me), when activity ends, someobject
destroyed (assuming no other reference exists). activity references someobject
, not other way round. don't see why leaked here, let alone entire activity.
it has fact they're creating anonymous class eventlistener.
public void onresume() { super.onresume(); someobject object = new someobject(); object.setsuccesslistener(new eventlistener<boolean>() { public void onevent(boolean response) { log.d(tag_name, "valid response? "+response); } }); someobjectmanager.getsingleton().addobject(object); }
it's explicitly stated being done in activity's onresume().
anonymous classes (as non-static inner classes) have implicit reference surrounding class. in case, eventlistener has reference activity itself.
therefore, someobject has reference activity because has reference anonymous class implements eventlistener.
here's text before quote referenced in question:
for instance, in example above: we’ve attached reference our activity instance object, presumably persistent, , in manager somewhere. point is...
so someobjectmanager class, not go away when activity gets destroyed, holds reference someobject, , someobject holds reference eventlistener, in turn references activity.
so when say:
as see (probably wrong; please correct me), when activity ends, someobject destroyed (assuming no other reference exists). activity references someobject, not other way round.
the flaw in logic someobject reference activity, through eventlistener.
does help?
Nice post thanks for sharing with us..
ReplyDeleteIf you have any requirement regarding App development like Android and iOS app development , so just click on link
top ios app development company in USA