In existing (presumably) working code I see the following construct:
void someFunc() {
some_irrelevant_code();
new Runnable() {
Handler handler = new Handler();
@Override
public void run()
{
//will run each 5 seconds
doSomething();
if (!needStopSomeThread)
{
handler.postDelayed(this, 5000);
}
}
}.run();
}
I wonder if the Runnable and the Handler may be garbage-collected at an arbitrary moment. It looks like only the runnable references the handler, and only the handler references the runnable, so they may be both garbage-collected, unless Android has a data structure referencing one of them, e.g. the handler.
1) Can they both (the handler and the runnable) get garbage-collected?
2) If they can get garbage-collected, how can I prove it actually happens?