Giter Club home page Giter Club logo

fragmentleaks's Introduction

Memory leaks in Fragments using Loaders

A sample project to demonstrate memory leak issues in the Android Support Library when using Fragments in combination with the Loader API.

Known issues

  • Since version 24.0.0 of the support library, a Fragment using a Loader which is detached will leak the Activity through the LoaderManager. Fragments are usually detached when switching between sections inside an app or when swiping in a ViewPager using a FragmentPagerAdapter.

  • Since way before that (maybe since forever), a Fragment using a Loader with setRetainInstance(true) will leak the Activity through the LoaderManager. The fragment does not need to be detached for the leak to occur.

Steps to reproduce

  • Launch this project on any device
  • Press the button to detach the fragment
  • Rotate the screen to trigger a configuration change
  • In the Memory Monitor in Android Studio, initiate a few garbage collections then press the button to dump the Java heap.
  • In the heap analyzer, look for the MainActivity class and notice there are two instances of it in memory instead of one.

LeakCanary analysis

Using the LeakCanary library from Square, the leak can be traced back to a variable named mHost in LoadermanagerImpl:

In be.digitalia.sample.fragmentleaks:1.0:1.
* be.digitalia.sample.fragmentleaks.MainActivity has leaked:
* GC ROOT static android.view.WindowManagerGlobal.sDefaultWindowManager
* references android.view.WindowManagerGlobal.mRoots
* references java.util.ArrayList.array
* references array java.lang.Object[].[0]
* references android.view.ViewRootImpl.mContext
* references be.digitalia.sample.fragmentleaks.MainActivity.mFragments
* references android.support.v4.app.FragmentController.mHost
* references android.support.v4.app.FragmentActivity$HostCallbacks.mAllLoaderManagers
* references android.support.v4.util.SimpleArrayMap.mArray
* references array java.lang.Object[].[1]
* references android.support.v4.app.LoaderManagerImpl.mHost
* references android.support.v4.app.FragmentActivity$HostCallbacks.this$0
* leaks be.digitalia.sample.fragmentleaks.MainActivity instance

Both issues produce the same leak trace so they seem to be caused by the same bug.

fragmentleaks's People

Contributors

cbeyls avatar

Watchers

 avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.