Android inner classes memory leak and leak by context?

Stella

I am using Handler in my splash screen for delaying redirection to the next activity as follows..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrance);
        screenTimeOut();
    }

    private void screenTimeOut() {
          /* New Handler to start the next screen
         * and close this Entrance after some seconds.*/
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                initTracker();
                /* Create an Intent that will start the Next-Activity. */
                checkLoginStatus();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

And in another activity am passing context to a class and holding the context inside that as follows on button click ..

 private Tools tools;

 tools = new Tools(DetailsScreen.this, true);

Tools

    private Context _context;
    private Fragment _fragment;
    private Activity activity;
    private String filePath = null;
    private String camImagePath = null;

    public Tools() {
    }

    public Tools(Context _context, boolean flag) {
        this._context = _context;
        this.activity = (Activity) _context;
        initPath();
        if (flag) {
            createImageFile();
        }
    }

Is any one of these will be a reason for leakage ?

And how about using handler as follows..

 private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrance);
        screenTimeOut();
    }

 private void screenTimeOut() {
          /* New Handler to start the next screen
         * and close this Entrance after some seconds.*/
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initTracker();
                /* Create an Intent that will start the Next-Activity. */
                checkLoginStatus();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
Henry
  1. Handler and Runnable should not be used in anonymous fashion.
  2. You should avoid passing the whole activity as a parameter to other classes. If context is all that you want use activity.getContext().

More info here:
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html http://www.androiddesignpatterns.com/2013/04/activitys-threads-memory-leaks.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

From Dev

"Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

From Dev

Android animations memory leak

From Dev

Is it a memory leak in android

From Dev

memory leak with Android WebView

From Dev

Is it a memory leak in android

From Dev

Android - is this a memory leak?

From Dev

Prevent memory leak in Android

From Dev

Android context memory leak via activity private member

From Dev

Possible to free Context from Singleton in Android to prevent memory leak

From Dev

Possible to free Context from Singleton in Android to prevent memory leak

From Dev

Android - Device Memory Leak with Fragments

From Dev

Android Fragment Webview Memory Leak

From Dev

Android runOnUiThread causing memory leak

From Dev

Android camera Bitmap memory leak

From Dev

Android progress bar memory leak

From Dev

`Unknown` (`Other`) memory leak in Android?

From Dev

Android runOnUiThread causing memory leak

From Dev

Getting Memory leak on android fragment

From Dev

Android memory leak Custom view

From Dev

Android memory leak on device, not on emulator

From Dev

Android Memory Leak - Anonymous class

From Dev

Avoid memory leak with WeakReference Android

From Dev

Is this a memory leak

From Dev

Is there a memory leak?

From Dev

How to release memory in android to avoid memory leak

From Dev

Android memory leak on textview - LeakCanary (Leak can be ignored)

From Dev

Will passing context to helper class in android activity leak?

From Dev

Android Does my app have a memory leak?

Related Related

HotTag

Archive