I'm playing around in Android Studio to learn more about memory leaks.
What i noticed is that, after rotating the screen a couple of times, i see multiple instances of that activity (after clicking on "Initiate GC" and "Dump Java Heap").
But when i click 2 times on "Initiate GC", and click after that on "Dump Java Heap", i see that my activity and its inner classes have only 1 instance.
Why do i have to click twice on "Initiate GC" to clear the activity instances? Am I leaking memory or not?
Edit: I also noticed this happens when creating a new project with a blank activity. So i'm probably not leaking any memory, but i'd still like to know why the instances aren't destroyed on first GC
The "Initiate GC" button is an signal to the GC to run. When Java GC runs, it is not guaranteed that all the memory references that can be cleared, will be cleared at this GC run.
This is only a trigger to GC to run. When GC runs by itself has the same behavior.
So you have to click multiple times the "Initiate GC" until you verify that the object you are afraid you are leaking has been cleared by GC.
There are plenty info out there, for example this thread How to force garbage collection in Java?