You must understand that System.gc(); does not call the garbage collector. He just politely asks GC to remove the garbage. GC decides what to do and when to start. Therefore, do not expect that you will immediately see the effect when you call System.gc(); by assigning null variable, etc.
GC deletes all objects that cannot be accessed. Therefore, if the code exits the block where the variable was defined, the object can be deleted. Applying a null value removes the link. A weak link does not prevent the GC from deleting the object.
I hope this explanation helps.
Alexr
source share