From fdd8b76add39eab6e2d0f492690bac0d166026d6 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 14 Jul 2023 18:32:04 +0200 Subject: [PATCH 1/4] Fix DebugApp doing unneeded AppWatcher.manualInstall --- app/src/debug/java/org/schabi/newpipe/DebugApp.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/debug/java/org/schabi/newpipe/DebugApp.kt b/app/src/debug/java/org/schabi/newpipe/DebugApp.kt index e5b308821..70b9ec280 100644 --- a/app/src/debug/java/org/schabi/newpipe/DebugApp.kt +++ b/app/src/debug/java/org/schabi/newpipe/DebugApp.kt @@ -3,7 +3,6 @@ package org.schabi.newpipe import androidx.preference.PreferenceManager import com.facebook.stetho.Stetho import com.facebook.stetho.okhttp3.StethoInterceptor -import leakcanary.AppWatcher import leakcanary.LeakCanary import okhttp3.OkHttpClient import org.schabi.newpipe.extractor.downloader.Downloader @@ -13,8 +12,6 @@ class DebugApp : App() { super.onCreate() initStetho() - // Give each object 10 seconds to be GC'ed, before LeakCanary gets nosy on it - AppWatcher.manualInstall(this, retainedDelayMillis = 10000) LeakCanary.config = LeakCanary.config.copy( dumpHeap = PreferenceManager .getDefaultSharedPreferences(this).getBoolean( From 135f0f7249a3ad3a974e376aa648662f979caedf Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 14 Jul 2023 18:32:30 +0200 Subject: [PATCH 2/4] Make all leak canary libs debugImplementation-only --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 810d4f652..00e7a8574 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -288,8 +288,8 @@ dependencies { /** Debugging **/ // Memory leak detection - implementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}" - implementation "com.squareup.leakcanary:plumber-android:${leakCanaryVersion}" + debugImplementation "com.squareup.leakcanary:leakcanary-object-watcher-android:${leakCanaryVersion}" + debugImplementation "com.squareup.leakcanary:plumber-android:${leakCanaryVersion}" debugImplementation "com.squareup.leakcanary:leakcanary-android-core:${leakCanaryVersion}" // Debug bridge for Android debugImplementation "com.facebook.stetho:stetho:${stethoVersion}" From 00257e969e009590a0ef11fca7bbc3e4e6269aa0 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 14 Jul 2023 18:34:20 +0200 Subject: [PATCH 3/4] Fix PlayerService leakead by Binder instance Also see https://stackoverflow.com/q/63787707 --- .../org/schabi/newpipe/player/PlayerService.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerService.java b/app/src/main/java/org/schabi/newpipe/player/PlayerService.java index d81301205..ad6c9405d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerService.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerService.java @@ -31,6 +31,8 @@ import android.util.Log; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.util.ThemeHelper; +import java.lang.ref.WeakReference; + /** * One service for all players. @@ -41,7 +43,7 @@ public final class PlayerService extends Service { private Player player; - private final IBinder mBinder = new PlayerService.LocalBinder(); + private final IBinder mBinder = new PlayerService.LocalBinder(this); /*////////////////////////////////////////////////////////////////////////// @@ -134,14 +136,19 @@ public final class PlayerService extends Service { return mBinder; } - public class LocalBinder extends Binder { + public static class LocalBinder extends Binder { + private final WeakReference playerService; + + LocalBinder(final PlayerService playerService) { + this.playerService = new WeakReference<>(playerService); + } public PlayerService getService() { - return PlayerService.this; + return playerService.get(); } public Player getPlayer() { - return PlayerService.this.player; + return playerService.get().player; } } } From 5d6158ea761b1c1594fa0a989d369e62f993bb4e Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 14 Jul 2023 20:48:05 +0200 Subject: [PATCH 4/4] No need to manually mark fragment as destroyed for LeakCanary It already does so automatically. --- app/src/main/java/org/schabi/newpipe/BaseFragment.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/BaseFragment.java b/app/src/main/java/org/schabi/newpipe/BaseFragment.java index e6478144b..19a99009b 100644 --- a/app/src/main/java/org/schabi/newpipe/BaseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/BaseFragment.java @@ -12,7 +12,6 @@ import androidx.fragment.app.FragmentManager; import icepick.Icepick; import icepick.State; -import leakcanary.AppWatcher; public abstract class BaseFragment extends Fragment { protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()); @@ -77,14 +76,6 @@ public abstract class BaseFragment extends Fragment { protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { } - @Override - public void onDestroy() { - super.onDestroy(); - - AppWatcher.INSTANCE.getObjectWatcher().expectWeaklyReachable( - this, "Watch for leaks from destroyed fragments."); - } - /*////////////////////////////////////////////////////////////////////////// // Init //////////////////////////////////////////////////////////////////////////*/