
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When will be an object eligible for garbage collection?
Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.
When an object created in Java program is no longer reachable or used it is eligible for garbage collection.
Following are some scenarios where a Java object could be unreachable/unused.
Object inside a method − In Java a method is stored in the stack memory. When you call a method, JVM fetches it into the stack and executes it. After the execution it is popped out of the stack then, all its variables will be discarded.
Therefore, when you create an object with in a method after the execution of the method this object becomes unreachable.
Example
public class Sample { String name; Sample(String name){ this.name = name; } public static void sampleMethod(){ Sample obj1 = new Sample("myObject2"); Sample obj = new Sample("myObject1"); } protected void finalize() throws Throwable { System.out.println(this.name + " successfully garbage collected"); } public static void main(String args[]){ sampleMethod(); System.gc(); } }
Output
myObject1 successfully garbage collected myObject2 successfully garbage collected
Reassignment − When you reassign a value (another object) to an existing reference variable, the original object ends up being unreachable.
Example
public class Sample { String name; Sample(String name){ this.name = name; } protected void finalize() throws Throwable { System.out.println(this.name + " successfully garbage collected"); } public static void main(String args[]){ Sample obj = new Sample("myObject1"); obj = new Sample("myObject2"); System.gc(); } }
Output
myObject1 successfully garbage collected
Assigning null − When you assign a null value to a reference of an object it is no longer reachable.
Example
public class Sample { String name; Sample(String name){ this.name = name; } protected void finalize() throws Throwable { System.out.println(this.name + " successfully garbage collected"); } public static void main(String args[]){ Sample obj = new Sample("myObject1"); obj = null; System.gc(); } }
Output
myObject1 successfully garbage collected
Anonymous object − Actually, the reference of the anonymous object will not be stored anywhere therefore, after the first execution you cannot call it again hence, it is unreachable.
Example
public class Sample { String name; Sample(String name){ this.name = name; } public void sampleMethod(){ System.out.println("This is a sample method"); } protected void finalize() throws Throwable { System.out.println(this.name + " successfully garbage collected"); } public static void main(String args[]){ new Sample("object1").sampleMethod(); System.gc(); } }
Output
This is a sample method object1 successfully garbage collected
- Related Questions & Answers
- How to make an object eligible for garbage collection in Java?
- When are python objects candidates for garbage collection?
- Java Garbage collection
- When will be an IllegalStateException (unchecked) thrown in Java?
- How many ways to make an object eligible for GC in Java?
- Garbage collection in Java
- Garbage Collection in Python
- What is JavaScript garbage collection?
- Garbage collection(GC) in JavaScript?
- When will 5G services be launched in India?
- What is garbage collection in C#?
- Destructors and Garbage Collection in Perl
- Destroying Objects (Garbage Collection) in Python
- How to prevent object of a class from garbage collection in Java?
- How does garbage collection work in Python?