NativeScript / NativeScript/android
Memory Leak on Navigation due to references not being removed from the HashMap
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 563
- Forks
- 144
- Avg merge
- 10h 46m
- Merged PRs (30d)
- 14
Description
Environment
- CLI: 8.0.2
- Cross-platform modules:
- Android Runtime: 8.0.0
- iOS Runtime (if applicable):
- Plugin(s):
Describe the bug
When navigating the old fragments are still being referenced in the HashMap so the Garbage Collector doesn't collect them causing a memory leak.
When the navigation's 'clearHistory: true' the backstack is emptied properly but the memory leak remains.
When 'clearHistory: false' you can see all the old pages still in the backstack with different Id names even if they are the same page.
To Reproduce
ns create my-drawer-js --template @nativescript/template-drawer-navigation
Open up the profiler in Android Studio
Go to Memory and start a new profiling session
Navigate to a different page in the template app
Go back to profiler and Dump Java Heap

Click the Leaks link

Click the FragmentClass

Click the Instance

Click the References Tab and check the 'Show nearest GC root only' Option

The FragmentClass instance can't be released because of the references in the HashMap I believe.
This leak is also caught using leak canary

Expected behavior
Fragments should be getting completely removed from memory
Sample project
I used the most vanilla template I could to make sure issues weren't coming from frameworks or plugins
https://www.npmjs.com/package/@nativescript/template-drawer-navigation
Additional context
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the leak with @nativescript/template-drawer-navigation, then inspect the Android Studio heap dump and LeakCanary references described in the issue. Trace the FragmentClass instances retained by the HashMap during navigation and verify that cleared or replaced pages can be garbage-collected without retaining old fragments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android
- Domain
- mobile, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100