Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Android kill my app while it is in the middle of a loop execution?

When Android decides to remove an application from the stack in order to free up some RAM, what happens if the application that is being destroyed is currently running some loop in the background? Will the loop be terminated amid execution or will the VM wait for it to finish?

like image 653
Kaloyan Roussev Avatar asked Sep 26 '22 20:09

Kaloyan Roussev


1 Answers

Will the loop be terminated amid execution or will the VM wait for it to finish?

The loop is terminated, otherwise it isn't quite "killing".

Simple test:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    Log.i("LOOP", "Running");
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {}
        }
    }).start();
}
}

Swipe the app out from the recent apps.

like image 55
Onik Avatar answered Oct 05 '22 21:10

Onik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!