How does the lifecycle of an object work, and explain the concept of garbage collection or manual destruction at a high level?
Last updated
Lifecycle: memory is allocated, a constructor initializes it, it is used while reachable, then it is reclaimed.
Garbage-collected languages (Java, C#, Python) automatically free objects unreachable from roots. Manual languages (C++) require
delete, which runs a deterministic destructor but risks leaks or double-frees.GC timing is non-deterministic, so release files/sockets with explicit patterns (
try-with-resources,using,with, RAII) instead of finalizers.