Fragmented - Android Developer Podcast

025: Effective Java for Android developers : Item 7


Listen Later

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
  • 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-closeables. [Android] devs can only dream of these.
        • If an uncaught exception is thrown in a finalizer, it is ignored, and the finalization abruptly terminates.
        • Severe performance penalty for using finalizers - (one e.g.) time to create and destroy simple object goes from 5.6ns -> 2400ns
        • Only valid use: as a safety net or to terminate noncritical native resources.
        • [Android] you're probably better off using Android's lifecycle methods.
        • Contact
          • @fragmentedcast [twitter.com]
          • @donnfelker [twitter.com]
          • @kaushikgopal [twitter.com]
          • ...more
            View all episodesView all episodes
            Download on the App Store

            Fragmented - Android Developer PodcastBy Donn Felker, Kaushik Gopal

            • 5
            • 5
            • 5
            • 5
            • 5

            5

            68 ratings


            More shows like Fragmented - Android Developer Podcast

            View all
            Design Details by Brian Lovin, Marshall Bock

            Design Details

            361 Listeners

            Developer Tea by Jonathan Cutrell

            Developer Tea

            402 Listeners

            Does Not Compute by Sean Washington, Rockwell Schrock

            Does Not Compute

            53 Listeners

            Toolsday by Una Kravets, Chris Dhanaraj

            Toolsday

            48 Listeners

            Swift Unwrapped by JP Simard, Jesse Squires, Spec Network, Inc.

            Swift Unwrapped

            90 Listeners