025 – Effective Java for Android developers : Item 7
In this mini Fragment, we introduce Joshua's seventh Item and a momentous end to the first chapter: Avoid finalizers
Stay tuned for more items from our "Effective Java for Android developers" Fragment series.
Show notes for this episode:
Effective Java (2nd Edition) - Joshua Bloch
Avoid finalizers
If you don't know what they are, ignorance is bliss. If you know what they are, avoid them!
Finalizers in Java != destructors in C++ (C++ counterparts to constructors).
In C++ destructors
you reclaim resources here (Java has GC)
you also reclaim non-memory resources (use the try-finally block in Java)
(unpredicatable amt of time between object becoming unreachable and finalizer being executed) Never do anything time critical in finalizer!
System.gc + System.runFinalization increase chances - no guarantee
System.runFinalizersOnExit + Runtime.runFinalizersOnExit are the ones that do - but they are fatally flawed
Java 7 has try with resources, which is also interesting and auto-closeabl
Comments