Question: how garbage collection works in java?
Answer: All objects are allocated on the heap area managed by the JVM. ... As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.