2

I am developing an Android app with fragments (=screens) with help of the new Navigation components library.

I have screen [A] and screen [B]. Screen [A] contains items, and screen [B] more details about a specific item. So the user will always switch between screen [A] and [B].

I've discovered that the memory consumption with each opened screen [B] rises and doesn't decrease to previous levels anymore. Even when I force a garbage collection the memory is still way to high. For reference, I get a memory consumption of 100 MB for clicking on approximately 20 items (so it seems like the 20 opened fragments/screens [B] are still hold in memory for some reason).

In Screen [B] I use this line to navigate back to [A].

NavHostFragment.findNavController(this).popBackStack();

So my question is, how do I tell my App to not hold on to these old fragments? Just popping them off the backstack doesn't seem to be enough.

user123456789
  • 301
  • 1
  • 4
  • 15
  • I don't know the answer, but did you try to use [LeakCanary](https://square.github.io/leakcanary/)? It detects memory leaks in Android apps. Good question, I was looking for an answer to your question, but sadly no clear answer about the memory consumption of fragments yet. – caveman Sep 26 '22 at 00:07
  • I am not sure but this might help: [FragmentManager popBackStack doesn't remove fragment](https://stackoverflow.com/questions/23499198/fragmentmanager-popbackstack-doesnt-remove-fragment) – Abdul Aziz Barkat Sep 26 '22 at 04:44

0 Answers0