java - Fragment objects after the fragment is destroyed; when GC collects them? -
i have fragment, , inside fragment start worker thread, can take few seconds. after worker thread finished, have insert sqlite database calling mdatabasehandler.insertsomething(something) (mdatabasehandler private instance inside fragment).
inside worker thread don't access views created ui thread, access objects created in fragment. happens if remove fragment or destroy it? i've read when fragment removed ondestroyview called. i'm not sure happens other objects. tried operations in ondestroy method of fragment:
@override public void ondestroy() { super.ondestroy(); client client = mdatabasehandler.getthelastclient(); log.e(tag, "client full name: " + client.getfullname()); }
everything worked fine. final question is: when gc collect objects created in fragment after fragment destroyed?
as keeping reference object in fragment. fragment not garbage collected until thread finished or releases reference object in fragment. if object has strong reference, gc not collect it. in case, keeping reference object in fragment, hence implicit reference fragment object , leaking memory (now garbage collector not able reclaim memory occupied fragment)
you should keep weak reference
object , while using object, check if still available or not (either weak reference returns object or null).
you can read more weak reference here.
Comments
Post a Comment