A Master guide to Analyse Memory Leaks in Android — Part 2— Profile, Strict Mode, Leak Canary
For Android Engineers
A Master guide to Analyse Memory Leaks in Android — Part 2— Profile, Strict Mode, Leak Canary

Memory leaks are a critical issue in Android apps, as they can lead to performance issues, crashes, and excessive battery draining. In this part of the blog, we will explore three powerful tools to help you find and analyze memory leaks in your Android applications:
This is the second part in the Memory leak series. Please refer first part from here. — Memory Leak in Android.
- Android Profiler
- StrictMode
- LeakCanary
1. Android Profiler
The Android Profiler is a set of tools in Android Studio that helps you monitor the performance of your app, including CPU, memory, and network usage. The memory profiler can be used to track memory usage and identify potential memory leaks.
Steps to Use Android Profiler:
- Open the Profiler:
- In Android Studio, click on View > Tool Windows > Profiler to open the Profiler window.
- Alternatively, you can click the Profiler tab at the bottom of Android Studio.
2. Start the Profiler:
- Select the device or emulator you want to profile from the top of the screen.
- Once selected, you’ll see the live CPU, memory, and network usage graphs.
3. Track Memory Usage:
- Click on the Memory tab to view memory usage over time.
- The memory profiler will display a graph of heap size and allocated objects.
4. Analyze Allocations:
- To detect potential memory leaks, look for spikes in the memory usage that don’t go back down after performing a task.
- You can also click the Record Memory Allocations button to track when specific objects are allocated.
- If you observe that memory usage continuously increases with no corresponding decrease, it could indicate a memory leak.
5. Heap Dump:
- Click on Dump Java Heap to take a snapshot of the app’s memory.
- In the heap dump, look for unexpected object retention or objects that shouldn’t be alive anymore. For example, if
ActivityorFragmentinstances are still in memory after the user has navigated away, it may indicate a leak.
What to Expect:
- The Memory graph will show memory usage patterns.
- If your app has memory leaks, you’ll see abnormal growth in memory usage that doesn’t get released over time.
2. StrictMode
StrictMode is a developer tool in Android that helps you detect potential performance issues, including memory leaks. It runs in the background and detects operations that can be harmful to the performance of your app, such as disk and network operations on the main thread or the allocation of large amounts of memory.
Steps to Use StrictMode:
- Enable StrictMode:
- You can enable StrictMode in the
onCreate()method of yourApplicationorActivity. For detecting memory leaks, you'll want to enable memory leak detection.
import android.os.StrictMode
import android.app.Application
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// Enable StrictMode for detecting general memory leaks
val policy = StrictMode.VmPolicy.Builder()
.detectLeakedActivities() // Detect leaked activities
.detectLeakedRegistries() // Detect leaked Broadcast Receivers, Content Providers, etc.
.detectLeakedViews() // Detect leaked Views (e.g., holding a reference to a View outside its lifecycle)
.detectLeakedClosableObjects() // Detect any closable objects that are not closed
.penaltyLog() // Log violations to Logcat
.penaltyDialog() // Show a color notification to indicate violations
.build()
StrictMode.setVmPolicy(policy)
}
}
Run Your Application:
- Ensure that StrictMode is properly configured in your app (as shown in the code example above).
- Build and run your app on a physical device or emulator.
Monitor Logcat:
- Open Logcat in Android Studio to monitor the logs.
- You will see StrictMode warnings whenever a memory leak or improper memory handling is detected.
What to Expect:
Once StrictMode is enabled, you can expect to see Logcat warnings related to memory issues, including:
- Leaked Activities: — If an
Activityis held in memory after it is finished, you'll see a message like:
StrictMode policy violation: Leaked Activity detected
- This indicates that the activity was not properly cleaned up and was retained in memory.
Leaked SQLite Objects:
- If a SQLite object, like
SQLiteDatabaseorCursor, is not closed properly, you’ll get a warning such as:
StrictMode policy violation: Leaked SQLite object detected
- This shows that the database or cursor was left open, which can lead to memory leaks.
Leaked Closeable Objects:
- If resources such as file streams, sockets, or database cursors (objects that implement
java.io.Closeable) are not closed, you might see:
StrictMode policy violation: Leaked closeable object detected
- These warnings indicate that a resource was not properly released, preventing it from being garbage collected.
Leaked Views:
- If a
Viewreference is held outside of its lifecycle (e.g., by a static reference), you might see:
StrictMode policy violation: Leaked View detected
Leaked Registries (Broadcast Receivers, Content Providers, etc.):
- If any
BroadcastReceiver,ContentProvider, or other registered components are leaked, you may see:
StrictMode policy violation: Leaked registry object detected
What Happens Next:
- In Logcat: StrictMode will print detailed messages, including the stack trace, that describe what caused the memory leak or violation. For instance, it will show information about leaked objects, including the type of object and where it was created.
On the Device (if penaltyDialog() is used):
- If you have configured the
penaltyDialog()penalty action, you will see a color notification dialog appear on your device, notifying you in real-time that a violation has occurred. This visual cue will make it easier for you to spot violations during testing.
Key Takeaways:
- StrictMode helps you catch resource leaks, such as unclosed database connections, file streams, and
Activityreferences, which are common causes of memory leaks in Android apps. - By monitoring Logcat and observing StrictMode warnings, you can quickly identify potential memory leaks and address them.
- The color notification dialog triggered by
penaltyDialog()helps alert you to violations in real-time, making it easier to catch issues as they happen during development.
3. LeakCanary
LeakCanary is a popular open-source library designed specifically to detect memory leaks in Android applications. It automatically tracks memory allocations and notifies you if it detects a leak in your app.
Steps to Use LeakCanary:
- Add LeakCanary to Your Project:
- In your
build.gradlefile, add the LeakCanary dependency:
Keep this for testing environment only.
dependencies {
testImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
}
- Initialize LeakCanary:
- LeakCanary is automatically initialized once you add it as a dependency, but you can configure it as follows:
import com.squareup.leakcanary.LeakCanary
import android.app.Application
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// LeakCanary will automatically start once added as a dependency
if (LeakCanary.isInAnalyzerProcess(this)) {
return
}
LeakCanary.install(this)
}
}
- Run Your Application:
- When LeakCanary is initialized, it will automatically detect memory leaks and show a notification whenever it finds one in your phone.
2. Monitor Leaks:
- Once a leak is detected, LeakCanary will display a notification in the notification bar and provide details of the leak.
- You can also view the leak trace in the Logcat or the LeakCanary UI, which provides a detailed heap dump showing what caused the leak.
3. Analyze Leak:
- The heap dump will show all the objects that are being retained and help you trace which object is holding the reference and causing the leak.
What to Expect:
- LeakCanary will notify you of any detected leaks with detailed information in the form of heap dumps.
- You’ll get a clear overview of what objects are being leaked and where the references are coming from.
Below are the screenshots from the canary leak. You can use this to identify which place memory leak issue is there.

Summary
- Android Profiler: Use the memory profiler in Android Studio to track memory usage and take heap dumps. Monitor for unexpected increases in memory usage that don’t get released over time.
- StrictMode: Enable StrictMode to automatically detect leaks related to SQL objects and other resources, and view the warnings in Logcat.
- LeakCanary: Integrate LeakCanary into your app to automatically detect and report memory leaks, providing detailed information on the source of the leak.
By using these tools, you can easily identify and address memory leaks early, improving your app’s performance and stability. Happy coding!!
메타데이터
- post_id
- 12aa2f20ae9b
- slug
- a-master-guide-to-analyse-memory-leaks-in-android-part-2-profile-strict-mode-leak-canary-12aa2f20ae9b
- url
- https://medium.com/@manishkumar_75473/a-master-guide-to-analyse-memory-leaks-in-android-part-2-profile-strict-mode-leak-canary-12aa2f20ae9b
- canonical_url
- https://medium.com/@manishkumar_75473/a-master-guide-to-analyse-memory-leaks-in-android-part-2-profile-strict-mode-leak-canary-12aa2f20ae9b
- author_url
- https://medium.com/@manishkumar_75473
- status
- ok
- fetched_at
- 2026-08-10 00:05:03