Object lifecycle
An object goes through several stages from creation to destruction.
1. Declaration – reference variable is created on the stack (no object yet).
Person p; // reference variable, null2. Instantiation – memory is allocated on the heap using new.
p = new Person(); // object created3. Initialization – constructor runs, fields are initialized.
4. Usage – object is used via its reference.
p.setName("Alice");String name = p.getName();5. Becoming unreachable – when no references point to the object.
p = null; // object becomes unreachable// or method exits, local reference goes out of scope6. Garbage collection – JVM automatically frees memory (nondeterministic time).
7. Finalization (deprecated) – historically, finalize() was called before collection. Now discouraged.
8. Memory reclamation – memory is returned to the heap for reuse.
Diagram
Section titled “Diagram”Declaration → Instantiation → Initialization → Usage → Unreachable → GC → Reclamation
Best practices:
- Minimize object creation in performance‑critical loops.
- Use object pooling for expensive objects.
- Let GC handle memory; avoid manual interference.
- Use
try‑with‑resourcesfor external resources.