I've got a question about garbage collector and Object.finalize(). I created a class which implements the finalize-method. If I run my app and force GC in DDMS finalize is called on objects of my class with no reference left. But if I call System.gc() within my app finalize is not called. Calling System.exit(0) when leaving the app doesn't cause finalize neither. Why is finalize not called?
Asked
Active
Viewed 2,761 times
1
-
GC does the the job for you. So there is no need to call `System.gc()` or `System.exit(0)`. Check on stackoverflow there is answer by commonsware on the topic. her's the link http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon. do not use `System.exit(0)` – Raghunandan Sep 29 '13 at 12:00
-
1I too recently answered a similar q: http://stackoverflow.com/questions/19071340/freeing-shared-resources-in-android-app/19072000#19072000 – cYrixmorten Sep 29 '13 at 12:01
1 Answers
1
But if I call System.gc() within my app finalize is not called.
That is because your object was not garbage-collected. gc()
does not do a complete GC. Instead, it collects some garbage, then returns.
Calling System.exit(0) when leaving the app doesn't cause finalize neither.
That is because your process is being terminated. Finalizers are not run on process termination. And, as Raghunandan noted, please do not call System.exit(0)
.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491