Android Memory Leaks InputMethodManager Solved

Main Activity is not garbage collected even after finish because it is referenced by InputMethodManager indirectly .

Many times we see that when an android app is closed (means the last activity is finished ), we get that the main activity is not garbage collected as is is referenced by InputMethodManager indirectly , mainly this issue comes when we use RecyclerView in the fragment associated with FragmentStatePagerAdapter in that activity , TreeObserver or something related with InputMethodManager being used.

Even after onDestroy() of that main activity is being called the app keeps the reference of that activity as it is referenced by InputMethodManager indirectly.

So, we cannot fix it, as it’s due the bug in Android Source code.

Link one

Link two

There can be some ways to fix it but we did not get the way to fix it . After some observations we realize that if any other activity is being called after that the finish of main activity the things is going perfect , no more Memory Leaks by InputMethodManager.

So we created the like this

package com.memory.test;

import android.os.Bundle;
import android.os.Handler;

 * Created by amitshekhar on 06/05/16.
public class DummyActivity extends AppCompatActivity {
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        new Handler().postDelayed(new Runnable() {
            public void run() {
        }, 500);

And a theme in styles.xml and added that in Androidmanifest.xml

  <!-- Customize theme for floating. -->
    <style name="AppTheme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:screenOrientation="portrait" />


And added the following code in

// Write this in onBackPress of your MainActivity
public class MainActivity extends AppCompatActivity{
    public void onBackPressed() {
       // This is important : Hack to open a dummy activity for 200-500ms (cannot be noticed by user as it is for 500ms
       //  and transparent floating activity and auto finishes)
       startActivity(new Intent(this, DummyActivity.class));

In this way , Dummy activity is being called after the Main Activity but cannot be noticed as 500ms is too small and even the dummy activity is transparent , so no chance of any side-effect or being noticed.

LeakCanary (A powerful library for finding memory leaks) : The developers of LeakCanary has also mentioned to ignore this leaks (Link) as it’s a bug in android source code.

But this is the way that we found to fix the memory leak created by InputMethodManager in MainActivity.

Hope this helps for now.

Happy Bug Fixing For Now :)

You can connect with me here.

Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.