Once android application terminates (closed either by user or by OS), is leaked memory and all other memory resources being freed? Or we need to do it manually? What will be the best way to track and handle memory issues in android?
-
1What does "application terminates" mean? If the application process is killed, then all memory is reclaimed. Any "leaked" memory is leaked inside the process. (After your edit). Killed by user means task killer or swiping out of recent apps list? – Simon Jan 06 '14 at 12:19
-
closed either by user or by OS(unexpectedly). – Daud Arfin Jan 06 '14 at 12:20
-
Any one of the case either closed by task killer or unexpected closed(crashed) or normal activity destroy what ever the case. – Daud Arfin Jan 06 '14 at 12:26
2 Answers
a memory leak in a situation where some objects are not used by the application any more, but GC fails to recognize them as unused.
GC is automatically done periodically by the JVM.
An android application can only be terminated by the OS. (safely at least)
if the app is closed by the user, it still runs in the background, once the os decides that it needs to close the application, either to free up some memory or the application stack is full, it will terminate the application and the memory will be freed.
If the application is terminated, all resources used by the application is freed.
99.9% of the time you do not need to call garbage collection on android. The OS takes care of itself. Would probably cause more harm to manually call GC
There are some cases where memory is leaked, but there are workarounds to dispose things in these cases.

- 10,393
- 4
- 39
- 67
-
so you are saying at developer's point of view no need to worry about memories once application is terminated ? – Daud Arfin Jan 06 '14 at 12:29
-
Yeah, basically if the app is closed by the user, it still runs in the background, once the os decides that it needs to close the application, either to free up some memory or the application stack is full, it will terminate the application and free the memory. – string.Empty Jan 06 '14 at 12:32
-
@laalto if object have a reference then it wont be freed after termination ? – Daud Arfin Jan 06 '14 at 12:32
-
@laalto yes this is true, but after the application is closed by the user it is not necessary to garbage collect. – string.Empty Jan 06 '14 at 12:33
-
If the application is terminated, all resources used by the application is freed. end of story – string.Empty Jan 06 '14 at 12:33
-
If process terminates then Yes, but that does not happen very often. Android is designed to keep processes in background to start them quickly once user want to go back to you app.
You should not rely that your app will be terminated to fix some memory leaks. There are tools to fix them, like dumping HPROF file and using memory analyzer, also using weak references, and using good programming practicies - mostly not leaking activities.
[edit] - there are resources that are not always freed on process end, while working with android TTS apis, I found that after few app crashes I have to reset device to be able to use svox voices.

- 48,511
- 9
- 79
- 100
-
I agree with your second point but i was worrying if i am failed to do so will it be handled by os itself? – Daud Arfin Jan 06 '14 at 12:37
-
1Reference leaks will not be handled automatically during your app lifetime, after some time when there wont be more memory android will simple close your app showing Error to user. You dont want that, and remember that system can keep your app process in background for days. – marcinj Jan 06 '14 at 12:41