Memory leak in SharedDecksActivity (suspected WebView Context issue)
Checked for duplicates?
- [x] This issue is not a duplicate
Does it also happen in the desktop version?
- [ ] This bug does not occur in the latest version of Anki Desktop
What are the steps to reproduce this bug?
- Open AnkiDroid.
- Navigate to "Get Shared Decks" (SharedDecksActivity).
- Press the back button to return to the DeckPicker.
- Repeat steps 1-3 multiple times (e.g., 10-20 times).
- Inspect the memory usage using the Android Studio Profiler.
Observation The memory usage keeps increasing. Instances of SharedDecksActivity are not being garbage collected. The heap dump suggests that UiModeManager holds a reference to the WebView, which in turn holds the Activity Context.
I did it 7 times
I did it 24 times
Expected behaviour
SharedDecksActivity should be garbage collected after it is destroyed.
Debug info
AnkiDroid Version = 2.24.0alpha0-debug (120ae9e3e1cc9d74ae1cd9c93f87cf18d6a122d9)
Backend Version = 0.1.62-anki25.09.2 (25.09.2 3890e12c9e48c028c3f12aa58cb64bd9f8895e30)
Android Version = 14 (SDK 34)
ProductFlavor = play
Device Info = Google | google | emu64xa | sdk_gphone64_x86_64 | sdk_gphone64_x86_64 | ranchu
Webview User Agent = Mozilla/5.0 (Linux; Android 14; sdk_gphone64_x86_64 Build/UE1A.230829.036.A4; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/113.0.5672.136 Mobile Safari/537.36
ACRA UUID = cb9112dd-e24d-4e60-b2e8-a126d16276e7
FSRS = 5.1.0 (Enabled: false)
Crash Reports Enabled = false
AnkiDroid Version = 2.24.0alpha0-debug (120ae9e3e1cc9d74ae1cd9c93f87cf18d6a122d9)
Backend Version = 0.1.62-anki25.09.2 (25.09.2 3890e12c9e48c028c3f12aa58cb64bd9f8895e30)
Android Version = 15 (SDK 35)
ProductFlavor = play
Device Info = Google | google | emu64xa | sdk_gphone64_x86_64 | sdk_gphone64_x86_64 | ranchu
Webview User Agent = Mozilla/5.0 (Linux; Android 15; sdk_gphone64_x86_64 Build/AE3A.240806.036; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/141.0.7390.122 Mobile Safari/537.36
ACRA UUID = d3ddc623-c774-4c20-b707-be5792040f43
FSRS = 5.1.0 (Enabled: false)
Crash Reports Enabled = false
(Optional) Anything else you want to share?
I have been investigating this issue and I would like to share my findings and a potential solution for review.
My Analysis: It seems that defining the WebView in the XML layout causes it to be initialized with the Activity Context. This appears to prevent the Activity from being garbage collected because system services (like UiModeManager) keep a reference to the WebView. Also there is no onDestory function in SharedDecksActivity.
Experimental Fix: I tried modifying the code to instantiate the WebView programmatically using the Application Context instead of the Activity Context. Added onDestroy(): Explicitly calling webView.stopLoading(), clearing the webViewClient, and calling webView.destroy().
Replaced
Request for Review: As I am a student and new to this codebase, I am not entirely sure if using applicationContext for the WebView might introduce other side effects (e.g., theming or dialog issues).
Could you please review if this approach is correct/safe?
Research
- [x] I have checked the manual and the FAQ and could not find a solution to my issue
- [ ] (Optional) I have confirmed the issue is not resolved in the latest alpha release (instructions)
Initialized WebView(applicationContext) in onCreate and added it to the container.
The application context is not ok to use with views, they should use an activity context.
Experimental Fix:
Check how we do things in SafeWebViewLayout.safeDestroy() and if using the same code in Activity.onDestroy() fixes the memory leak. If it doesn't then there's not much else we can do and we can close this.
Thanks for the feedback! I've read your suggestion. I'm a bit busy at the moment, so I'll test it using the SafeWebViewLayout.safeDestroy() approach and report back as soon as possible.
I implemented the onDestroy() method in SharedDecksActivity and incorporated the cleanup logic based on the pattern in SafeWebViewLayout.safeDestroy().
Changes:
- Implemented onDestroy() (which was previously missing in this Activity).
- Added a comprehensive WebView cleanup:
- Called stopLoading() and loaded about:blank.
- Nullified webChromeClient and OnScrollChangeListener.
- Detached the WebView from its parent ViewGroup.
- Finally, called webView.destroy().
Verification:
To verify the fix, I navigated between SharedDecksActivity and DeckPicker 10 times consecutively.
Result:
- API 34: some leaks resolved, but not all were eliminated.
- API 35: leaks are fully resolved with this cleanup in place.
API 34
API 35
I’d appreciate your thoughts on this approach — do you think it’s an acceptable solution given that the leak is now resolved?