Can we get a memory leak even if the app is killed

2019-05-12 08:18发布

问题:

I can not understand this statement from Activity.onStop():

When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

Specifically this part:

In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory

If the process is killed how can we get a memory leak if we don’t have release code in onStop? On app kill all resources are cleaned right?

回答1:

If the process is killed how can we get a memory leak if we don’t have release code in onStop?

You can't. The Android documentation gots issues, yo.

On app kill all resources are cleaned right?

Well, your process is terminated, which eliminates your RAM and threads. What you need to do is arrange to clean up anything that isn't tied to your RAM and threads. For example, if the user has entered data into the app that you want to keep and have not persisted yet, onStop() is a candidate time to consider forking the thread to save that stuff to disk.



回答2:

If process is killed. All associated memory content will be removed from the system so killing the process will not leak memory.