android - java - does GC try more then once to release memory? -
lets have asynctask this:
public void startasynctask() { new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { try { thread.sleep(300000); } catch (interruptedexception e) { e.printstacktrace(); } return null; } }.execute(); }
for dont know asynctask creates background thread. , anonymous classes non-static inner classes.
now lets in activity (main thread) call startasynctask()
the thread take little while complete (about 5 minutes).
during background threads lifetime, lets imagine activity gets destroyed , time garbage collect activity. since asynctask has implicit reference outer class (the activity) activity leak , not garbage collected.
but question following: after time, child thread finish. can gc attempt free memory again or activity forever leaked ? want know if child thread finishes in reasonable amount of time activities memory freed or on first attempt gc tries , after attempt memory forever lost ?
the garbage collector not operate based on activity life-cycle, operates based on "are there objects referencing it". yeah, activity memory freed after thread finishes (and asynctask not referencing anymore).
but still memory leak of heavy object (the activity). , it's bad programming shouldn't done.
i'm not sure if have actual problem want apply that, or purely theoretical question on inner workins of vm, if it's actual problem want apply that; answer is: don't it!
Comments
Post a Comment