Object Lifecycle in Java
The object lifecycle in Java describes the different stages an object passes through—from its creation to the point when its memory is reclaimed by the Java Virtual Machine (JVM).
A Java object generally passes through these stages:
Creation → Initialization → Usage → Unreachable State → Garbage Collection
Java manages object memory automatically. Developers create and use objects, while the JVM’s garbage collector reclaims the memory of objects that are no longer reachable.
1. Object Creation and Initialization
An object is usually created using the new keyword. The new operator allocates memory for the object and invokes its constructor to initialize it.
Object creation involves:
Declaration of a reference variable
Instantiation using the
newkeywordInitialization through a constructor
class Student { private String name; // Constructor initializes the object Student(String name) { this.name = name; } void displayName() { System.out.println("Student: " + name); }}public class Main { public static void main(String[] args) { // Creates and initializes a Student object Student student = new Student("Rahul"); // Uses the object student.displayName(); // Output :- Student: Rahul }}In this example:
studentis a reference variable.new Student("Rahul")creates the object.The constructor initializes the
nameattribute.The reference variable points to the newly created object.
Object Creation and Initialization
2. Object Usage
After creation, an object can be used to access its fields and call its methods.
student.displayName(); // Output :- Student: RahulAn object remains available while it can still be reached through an active reference.
Multiple reference variables can also point to the same object:
class Book { String title; Book(String title) { this.title = title; } void displayTitle() { System.out.println(title); }}public class Main { public static void main(String[] args) { Book book1 = new Book("Java Basics"); // book2 points to the same object as book1 Book book2 = book1; book2.displayTitle(); // Output :- Java Basics }}Only one Book object is created here, but both book1 and book2 refer to it.
Object Usage
3. Object Becomes Unreachable
An object becomes unreachable when the program can no longer access it through any active reference. Once an object becomes unreachable, it becomes eligible for garbage collection.
An object may become unreachable when:
Its reference is assigned
null.Its reference is replaced with another object.
A local reference goes out of scope.
An object is created without storing its reference.
Example
class Employee { void work() { System.out.println("Employee is working"); }}public class Main { public static void main(String[] args) { Employee employee = new Employee(); employee.work(); // Output :- Employee is working // The original object no longer has an active reference employee = null; }}Assigning null does not immediately delete the object. It only makes the object eligible for garbage collection if no other reference points to it.
Object Becomes Unreachable
4. Garbage Collection
Garbage collection in Java is the JVM process that identifies unreachable objects and reclaims their memory.
Java uses reachability-based garbage collection. An object is considered garbage when it can no longer be reached from a live part of the application. The exact time at which its memory is reclaimed is controlled by the JVM.
Developers do not have to destroy objects manually, and Java does not provide a traditional destructor for normal object cleanup.
The System.gc() Method
The System.gc() method requests that the JVM run the garbage collector.
public class Main { public static void main(String[] args) { Object object = new Object(); object = null; // Requests garbage collection System.gc(); }}However, System.gc() does not guarantee that garbage collection will happen immediately. The JVM decides whether and when to perform it. Explicit garbage-collection requests should generally be avoided because they can unnecessarily affect application performance.
Garbage Collection in Java
Cyclic References
A cyclic reference occurs when objects refer to each other.
class Node { Node next;}public class Main { public static void main(String[] args) { Node first = new Node(); Node second = new Node(); // Both objects refer to each other first.next = second; second.next = first; // The objects can no longer be reached by the program first = null; second = null; }}Although the two objects reference each other, Java can still collect them because neither object is reachable from the running program.
Cyclic References in Java
Memory Leaks in Java
Java has automatic garbage collection, but memory leaks can still occur when a program keeps references to objects that are no longer needed.
Common reasons include:
Objects stored permanently in static collections
Listeners that are registered but never removed
Caches that grow without a limit
Long-lived references to unused objects
The garbage collector cannot remove an object while it remains reachable, even if the application no longer needs it.
Summary
The object lifecycle in Java begins when an object is created with the new keyword. The object is initialized through a constructor and used through its reference. When it is no longer reachable, it becomes eligible for garbage collection. The JVM can then reclaim its memory automatically.
Understanding this lifecycle helps developers manage references correctly, reduce memory leaks, and write more memory-efficient Java programs.
Be the first to add a comment.