Before I dig deep into the topic, below are the few points to remember:
- Java provides automatic memory management through a program named “Garbage Collector”.
- Remove objects that are not used anymore.
- live object = reachable(referenced by someone else)
- dead object = unreachable(not referenced from anywhere)
- Objects are allocated in…