How to understand what LeakCanary shows?

I installed Proetk LeackCanary in your library. He discovered a memory leak and gave me information about it, but I cannot understand it, because I do not have such practice in the classroom. How to understand exactly where the error is and how to fix it? Thanks.

public final class Activity extends AppCompatActivity {

InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fci); getWindow().setBackgroundDrawable(null); final ImageButton btn_pas = (ImageButton) findViewById(R.id.btn_pas); Glide.with(getApplicationContext()).load(R.drawable.fci_2).placeholder(R.color.white).into(btn_pas); mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-..."); requestNewInterstitial(); } @Override public void onBackPressed() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); super.onBackPressed(); } else { super.onBackPressed(); } requestNewInterstitial(); } private final void requestNewInterstitial() { AdRequest adRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adRequest); } public final void pas(View view) { Intent intent = new Intent(Activity.this, pas.class); startActivity(intent); } 

}

I can’t attach the screenshot, so I’ll describe the LeakCanary entry.

 static hk.o references ht.a leaks Activity instance 
+7
java android memory-leaks leakcanary
source share
1 answer

You are experiencing a leak because InterstitialAd keeps a link to activity. You must replace:

 mInterstitialAd = new InterstitialAd(this); 

from

 mInterstitialAd = new InterstitialAd(this.getApplicationContext()); 

For more information, see my answer on a similar question .

0
source share

All Articles