From 63fe1c49a122ae584d737e43daf278e4c8065db8 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Tue, 23 Apr 2019 10:09:45 +0200 Subject: [PATCH] swap out 2.0 --- CHANGELOG.md | 1 - README-2.0.md => README-1.6.md | 102 ++++++------------------------- README.md | 107 ++++++++++++++++++++++++++------- 3 files changed, 106 insertions(+), 104 deletions(-) rename README-2.0.md => README-1.6.md (59%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e06e5da1f..09990cb9dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ -* Check out the [2.0 Readme](https://github.com/square/leakcanary/blob/master/README-2.0.md). * New [logo](https://github.com/square/leakcanary/wiki/FAQ#who-made-the-logo), thanks [@flickator](https://github.com/flickator)! * Entirely rewritten to **100% Kotlin** * Multiple leaks detected in one analysis diff --git a/README-2.0.md b/README-1.6.md similarity index 59% rename from README-2.0.md rename to README-1.6.md index bbc7fb5df3..aea175515c 100644 --- a/README-2.0.md +++ b/README-1.6.md @@ -1,111 +1,49 @@ # LeakCanary -A memory leak detection library for Android and Kotlin. +A memory leak detection library for Android and Java. *“A small leak will sink a great ship.”* - Benjamin Franklin

- +

## Getting started -Add LeakCanary to your `build.gradle`: - -```groovy -dependencies { - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-1' -} -``` - -**You're good to go!** LeakCanary will automatically show a notification when an activity or fragment memory leak is detected in your debug build. - -## Presentations - -* [LeakCanary, then what? Nuking Nasty Memory Leaks](https://www.youtube.com/watch?v=fhE--eTEW84) -* [Memory Leak Hunt](https://www.youtube.com/watch?v=KwArTJHLq5g), a live investigation. - -## Recipes - -### Watching custom objects - -```kotlin -class MyService : Service { - - // ... - - override fun onDestroy() { - super.onDestroy() - LeakSentry.refWatcher.watch(this) - } - -} -``` - -### Configuring LeakSentry & LeakCanary - -LeakSentry is in charge of detecting memory leaks. Its configuration can be updated at any time by replacing `LeakSentry.config`: -```kotlin -class DebugExampleApplication : ExampleApplication() { - - override fun onCreate() { - super.onCreate() - LeakSentry.config = LeakSentry.config.copy(watchFragmentViews = false) - } -} -``` - -LeakCanary is in charge of taking heap dumps and analyzing them. Its configuration can be updated at any time by replacing `LeakCanary.config`: - -``` -disableLeakCanaryButton.setOnClickListener { - LeakCanary.config = LeakCanary.config.copy(dumpHeap = false) -} -``` - -### Counting retained instances in production - In your `build.gradle`: ```groovy dependencies { - implementation 'com.squareup.leakcanary:leakcanary-sentry:2.0-alpha-1' -} -``` - -In your leak reporting code: -```kotlin -val retainedInstanceCount = LeakSentry.refWatcher.retainedKeys.size -``` - -### Alternate setup with the old perflib heap parser - -If you want to try LeakCanary 2.0 features with the battle tested perflib heap parser, use a different dependency: - -```groovy -dependencies { - // debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-1' - debugImplementation 'com.squareup.leakcanary:leakcanary-android-perflib:2.0-alpha-1' + debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3' + releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3' + // Optional, if you use support library fragments: + debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3' } ``` -In your **debug** `Application` class: +In your `Application` class: -```kotlin -class DebugExampleApplication : ExampleApplication() { +```java +public class ExampleApplication extends Application { - override fun onCreate() { + @Override public void onCreate() { + super.onCreate(); if (LeakCanary.isInAnalyzerProcess(this)) { - // This process is dedicated to Perflib for heap analysis. + // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. - return + return; } - super.onCreate() + LeakCanary.install(this); + // Normal app init code... } } ``` +**You're good to go!** LeakCanary will automatically show a notification when an activity or support fragment memory leak is detected in your debug build. + +**What's next?** You could watch a [live investigation](https://www.youtube.com/watch?v=KwArTJHLq5g) then [customize LeakCanary](https://github.com/square/leakcanary/wiki/Customizing-LeakCanary) to your needs. + ## FAQ * [Why should I use LeakCanary?](https://github.com/square/leakcanary/wiki/FAQ#why-should-i-use-leakcanary) @@ -128,7 +66,7 @@ class DebugExampleApplication : ExampleApplication() { * [I know I have a leak. Why doesn't the notification show?](https://github.com/square/leakcanary/wiki/FAQ#i-know-i-have-a-leak-why-doesnt-the-notification-show)

- +

## License diff --git a/README.md b/README.md index aea175515c..fee7b5a020 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,116 @@ - # LeakCanary -A memory leak detection library for Android and Java. +A memory leak detection library for Android and Kotlin. *“A small leak will sink a great ship.”* - Benjamin Franklin

- +

## Getting started +Add LeakCanary to your `build.gradle`: + +```groovy +dependencies { + debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-1' +} +``` + +**You're good to go!** LeakCanary will automatically show a notification when an activity or fragment memory leak is detected in your debug build. + +Note: LeakCanary 2 is in alpha. To set up LeakCanary 1.6, go to the [1.6 Readme](https://github.com/square/leakcanary/blob/master/README-1.6.md). + +## Presentations + +* [LeakCanary, then what? Nuking Nasty Memory Leaks](https://www.youtube.com/watch?v=fhE--eTEW84) +* [Memory Leak Hunt](https://www.youtube.com/watch?v=KwArTJHLq5g), a live investigation. + +## Recipes + +### Watching custom objects + +```kotlin +class MyService : Service { + + // ... + + override fun onDestroy() { + super.onDestroy() + LeakSentry.refWatcher.watch(this) + } + +} +``` + +### Configuring LeakSentry & LeakCanary + +LeakSentry is in charge of detecting memory leaks. Its configuration can be updated at any time by replacing `LeakSentry.config`: +```kotlin +class DebugExampleApplication : ExampleApplication() { + + override fun onCreate() { + super.onCreate() + LeakSentry.config = LeakSentry.config.copy(watchFragmentViews = false) + } +} +``` + +LeakCanary is in charge of taking heap dumps and analyzing them. Its configuration can be updated at any time by replacing `LeakCanary.config`: + +``` +disableLeakCanaryButton.setOnClickListener { + LeakCanary.config = LeakCanary.config.copy(dumpHeap = false) +} +``` + +### Counting retained instances in production + In your `build.gradle`: ```groovy dependencies { - debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3' - releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3' - // Optional, if you use support library fragments: - debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3' + implementation 'com.squareup.leakcanary:leakcanary-sentry:2.0-alpha-1' +} +``` + +In your leak reporting code: +```kotlin +val retainedInstanceCount = LeakSentry.refWatcher.retainedKeys.size +``` + +### Alternate setup with the old perflib heap parser + +If you want to try LeakCanary 2.0 features with the battle tested perflib heap parser, use a different dependency: + +```groovy +dependencies { + // debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-1' + debugImplementation 'com.squareup.leakcanary:leakcanary-android-perflib:2.0-alpha-1' } ``` -In your `Application` class: +In your **debug** `Application` class: -```java -public class ExampleApplication extends Application { +```kotlin +class DebugExampleApplication : ExampleApplication() { - @Override public void onCreate() { - super.onCreate(); + override fun onCreate() { if (LeakCanary.isInAnalyzerProcess(this)) { - // This process is dedicated to LeakCanary for heap analysis. + // This process is dedicated to Perflib for heap analysis. // You should not init your app in this process. - return; + return } - LeakCanary.install(this); - // Normal app init code... + super.onCreate() } } ``` -**You're good to go!** LeakCanary will automatically show a notification when an activity or support fragment memory leak is detected in your debug build. - -**What's next?** You could watch a [live investigation](https://www.youtube.com/watch?v=KwArTJHLq5g) then [customize LeakCanary](https://github.com/square/leakcanary/wiki/Customizing-LeakCanary) to your needs. - ## FAQ +Note: the entries in this FAQ have not been updated for LeakCanary 2 yet. + * [Why should I use LeakCanary?](https://github.com/square/leakcanary/wiki/FAQ#why-should-i-use-leakcanary) * [How does it work?](https://github.com/square/leakcanary/wiki/FAQ#how-does-it-work) * [How do I fix a memory leak?](https://github.com/square/leakcanary/wiki/FAQ#how-do-i-fix-a-memory-leak) @@ -66,7 +131,7 @@ public class ExampleApplication extends Application { * [I know I have a leak. Why doesn't the notification show?](https://github.com/square/leakcanary/wiki/FAQ#i-know-i-have-a-leak-why-doesnt-the-notification-show)

- +

## License