Xamarin Android Finalizer not getting called when leaving the activity to go to another Activity

This is actually remarkably complicated; the short answer, regarding the activity still being alive, is yes and no. Providing you have cleaned up the resources for your Activity correctly, your activity will be cleaned up (eventually) by the garbage collector. Regarding cleanup, it’s import to know that Xamarin discourages (slide 44 onwards) using finalizers. Here’s … Read more

Why do finalizers have a “severe performance penalty”?

Because of the way the garbage collector works. For performance, most Java GCs use a copying collector, where short-lived objects are allocated into an “eden” block of memory, and when the it’s time for that generation of objects to be collected, the GC just needs to copy the objects that are still “alive” to a … Read more

GC.Collect() not collecting immediately?

Objects with finalizers cannot be collected within a single garbage collection procedure. Such objects are moved to f-reachable queue, and remain there until finalizers are called. Only after that they can be garbage-collected. Following code is better, but you should not rely on it anyway: GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Also, throwing exceptions in finalizer seems too … Read more

GC.Collect() and Finalize

Ok, it’s known that GC implicitly calls Finalize methods on objects when it identifies that object as garbage. No no no. That is not known because in order to be knowledge a statement must be true. That statement is false. The garbage collector does not run finalizers as it traces, whether it runs itself or … Read more

Why is the finalize() method deprecated in Java 9?

Although the question was asking about the Object.finalize method, the subject really is about the finalization mechanism as a whole. This mechanism includes not only the surface API Object.finalize, but it also includes specifications of the programming language about the life cycle of objects, and practical impact on garbage collector implementations in JVMs. Much has … Read more