I believe to have successfully installed LeakCanary.
I added the debug, release, and test dependencies to the build.gradle file.
I added the necessary files to my Application Class. Imported as necessary. Confirmed the application class is properly added to manifest. Does my application class need to be explicitly called?
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
I run my app on the emulator and don't see anything different. I monitor the Android Monitor and don't see any difference. How do I know if it's all working? I've shared my Application class.
import android.app.Application;
import android.content.res.Configuration;
import com.squareup.leakcanary.LeakCanary;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
return;
}
LeakCanary.install(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
}
Disable the leakcanary launcher activity by setting the leak_canary_add_launcher_icon resource boolean to false.
LeakCanary hooks into the Android lifecycle to automatically detect when activities and fragments are destroyed and should be garbage collected. These destroyed objects are passed to an ObjectWatcher , which holds weak references to them.
LeakCanary is a memory leak detection library for Android. LeakCanary's knowledge of the internals of the Android Framework gives it a unique ability to narrow down the cause of each leak, helping developers dramatically reduce Application Not Responding freezes and OutOfMemoryError crashes. Get started!
The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.
Does my application class need to be explicitly called?
No.
How do I know if it's all working?
Leak something intentionally. For example, assign your launcher activity instance to a static
field.
First, check if you are attached to a debugger? LeakCanary ignores leak detection when debugging to avoid false positives.
Second, Add the LeakCanary via Gradle and then do the following:
class App : Application() {
companion object {
@JvmStatic
fun getRefWatcher(context: Context): RefWatcher {
val applicationContext = context.applicationContext as App
return applicationContext.refWatcher
}
}
private lateinit var refWatcher: RefWatcher
override fun onCreate() {
super.onCreate()
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
refWatcher = LeakCanary.install(this)
}
}
In your Fragment (best you use a BaseFragment)
override fun onDestroy() {
if (BuildConfig.DEBUG) {
activity?.let {
App.getRefWatcher(it).watch(this)
}
super.onDestroy()
}
Then in one of your Sub-Activities with a Fragment do:
class MemLeakFragment : BaseFragment() {
companion object {
@JvmStatic
lateinit var memleak: Activity
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
memleak = it
}
}
}
Open the Activity with the memleak
Fragment and close it with a back press. Wait a bit and LeakCanary will report the memory leak, this can take a while...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With