Types of Objects and Garbage Collection
In Java, objects are the heart of object-oriented programming. Every time we create an instance of a class, an object is created in memory. These objects store data and allow methods to operate on that data.
As Java programs run, many objects are created, used, and eventually become unnecessary. Since memory is limited, Java must reclaim memory occupied by unused objects. This process is handled automatically through garbage collection.
What is an Object in Java?
An object is an instance of a class. It represents a real entity in memory and contains state and behavior.
Example:
Here:
Studentis a classsis a reference variablenew Student()creates an object in heap memory
Where are Objects Stored?
In Java:
- Objects are stored in heap memory
- Reference variables may exist in stack or as fields depending on where they are declared
Example:
Here:
sis the reference- actual object is in heap memory
Types of Objects in Java
In Java, objects can be discussed in different ways based on how they are created and how they exist in memory. Common practical categories include:
- Anonymous objects
- Named objects
- Referenced objects
- Unreferenced objects
- Eligible-for-garbage-collection objects
- Immutable objects
- Mutable objects
Let us understand them one by one.
1. Named Object
A named object is an object that is referenced through a variable name.
Here, the object has a reference variable s, so it is a named object.
2. Anonymous Object
An anonymous object is an object that is created without assigning it to a reference variable.
Example
This object is created but no reference variable stores it.
Anonymous objects are usually used when an object is needed only once.
Example with Method Call
Here, the object is created and immediately used to call show().
3. Referenced Object
Any object that is currently reachable through a valid reference variable is called a referenced object.
Since s points to the object, it is referenced and not eligible for garbage collection.
4. Unreferenced Object
An unreferenced object is an object that no longer has any valid reference pointing to it.
Once an object becomes unreferenced, it becomes eligible for garbage collection.
Example
Here, the original object created by new Student() becomes unreferenced after s = null;.
5. Mutable Object
A mutable object is an object whose internal state can be changed after creation.
Example
Since the value of name can change, the object is mutable.
6. Immutable Object
An immutable object is an object whose state cannot be changed after creation.
The most famous example in Java is the String class.
Example
Here, the original String object is not modified. Instead, a new object is created.
Object Creation in Java
Objects are most commonly created using the new keyword.
This process involves:
- memory allocation in heap
- default initialization of fields
- execution of instance variable initialization
- execution of instance blocks
- execution of constructor
Object Reference and Actual Object
Beginners often confuse the reference variable with the actual object.
sis not the object itselfsholds the reference to the object- the actual object lives in heap memory
Ways an Object Becomes Eligible for Garbage Collection
An object becomes eligible for garbage collection when it is no longer reachable by any live reference.
Common ways:
- nullifying the reference
- reassigning the reference
- objects created inside methods becoming unreachable after method ends
- isolated island objects
- anonymous objects after use
1. Nullifying a Reference
The object becomes unreachable because the reference now points to nothing.
2. Reassigning Reference
The first object becomes unreferenced because s now points to a new object.
3. Local Objects After Method Completion
Objects created inside a method may become unreachable once the method completes, if no reference escapes the method.
After createObject() finishes, the local reference s is destroyed, so the object becomes eligible for garbage collection.
4. Anonymous Objects After Use
Once the statement finishes, the object usually becomes unreachable because no reference is stored.
5. Isolated Island of Objects
An isolated island happens when a group of objects reference each other but none of them is reachable from outside.
Example
Here, both objects still refer to each other, but no external reference points to them. Therefore, they become eligible for garbage collection.
What is Garbage Collection?
Garbage collection is the automatic memory cleanup process in Java. The JVM identifies objects that are no longer reachable and reclaims their memory.
This removes the burden of manual memory management from the programmer.
Why Garbage Collection is Needed
- to free unused memory
- to prevent memory waste
- to reduce risk of memory leaks
- to simplify development
- to improve application stability
How Garbage Collection Works
The garbage collector works on the principle of reachability.
If an object is reachable through active references, it is kept alive.
If an object is unreachable, it becomes eligible for garbage collection.
Simple Rule
- Reachable object → not collected
- Unreachable object → eligible for collection
Can We Force Garbage Collection?
Java provides methods like:
System.gc()Runtime.getRuntime().gc()
But these methods only request garbage collection. They do not guarantee that garbage collection will happen immediately.
Example
What is finalize() Method?
The finalize() method was historically used to perform cleanup before an object was removed by the garbage collector.
Example:
However, in modern Java, finalize() is considered outdated and should generally be avoided.
Important Note About finalize()
Even in older Java versions, finalize() was not guaranteed to execute immediately or at all before program termination. Therefore, it should not be relied upon for important resource cleanup.
Modern Java prefers:
- try-with-resources
- explicit close methods
- cleaner resource management patterns
Example of Garbage Collection Flow
This program requests garbage collection after making both objects unreachable.
Garbage Collection is Automatic, Not Manual Memory Freeing
In Java, programmers do not free object memory directly like in C or C++. Java automatically handles memory cleanup through garbage collection.
This is one of the reasons Java is considered safer and easier for large-scale development.
Advantages of Garbage Collection
- automatic memory management
- reduced memory-related bugs
- less risk of dangling pointers
- simpler development process
- better safety compared to manual memory management languages
Limitations of Garbage Collection
- exact collection time is not guaranteed
- programmer has less low-level control
- poor object handling can still cause memory pressure
- unreleased external resources are not automatically managed like normal heap objects
Common Mistakes
- Thinking object reference and actual object are the same
- Believing
System.gc()guarantees immediate collection - Assuming two objects referencing each other can never be garbage collected
- Confusing memory cleanup with resource cleanup like files or DB connections
- Overusing objects unnecessarily in loops and large operations
Best Practices
- Create objects only when needed
- Remove unused references when appropriate
- Avoid holding references longer than necessary
- Use immutable objects where beneficial
- Manage external resources using try-with-resources instead of relying on garbage collection
Interview-Oriented Points
- Objects are stored in heap memory
- Anonymous objects have no reference variable
- Mutable objects can change state, immutable objects cannot
- Garbage collection removes unreachable objects automatically
- Objects become eligible for garbage collection when no live reference points to them
System.gc()only requests garbage collection- Isolated island objects are eligible for garbage collection
finalize()is outdated and should generally be avoided
Conclusion
Objects are the core runtime entities in Java, and understanding their lifecycle is essential for becoming a strong Java developer. Knowing how objects are created, referenced, modified, and eventually removed gives a much deeper understanding of Java memory behavior.
Garbage collection is one of Java’s most powerful features because it automatically reclaims memory from unused objects. This makes Java safer and easier to use than many low-level languages, especially in large applications.

