
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How many ways to make an object eligible for GC in Java?
The process of destroying unreferenced objects is called a Garbage Collection(GC). Once an object is unreferenced it is considered as an unused object, hence JVM automatically destroys that object.
There are various ways to make an object eligible for GC.
By nullifying a reference to an object
We can set all the available object references to "null" once the purpose of creating an object is served.
Example
public class GCTest1 { public static void main(String [] args){ String str = "Welcome to TutorialsPoint"; // String object referenced by variable str and it is not eligible for GC yet. str = null; // String object referenced by variable str is eligible for GC. System.out.println("str eligible for GC: " + str); } }
Output
str eligible for GC: null
By reassigning the reference variable to some other object
We can make the reference variable to refer to another object. Decouple the reference variable from the object and set it to refer to another object, so the object which was referring to before reassigning is eligible for GC.
Example
public class GCTest2 { public static void main(String [] args){ String str1 = "Welcome to TutorialsPoint"; String str2 = "Welcome to Tutorix"; // String object referenced by variable str1 and str2 and is not eligible for GC yet. str1 = str2; // String object referenced by variable str1 is eligible for GC. System.out.println("str1: " + str1); } }
Output
str1: Welcome to Tutorix
- Related Articles
- How to make an object eligible for garbage collection in Java?
- How many ways to call garbage collector (GC) in Java?\n
- When will be an object eligible for garbage collection?
- How many ways to synchronize an ArrayList in Java?\n
- How many ways a String object can be created in java?
- 3 ways to initialize an object in Java
- Different ways to create an object in java?
- How to make an object completely encapsulated in java?
- How many ways are there to convert an Array to ArrayList in Java?
- How many ways to iterate a LinkedList in Java?
- How many ways to iterate a TreeSet in Java?
- How many ways to prevent method overriding in Java?
- How many ways are there to register a driver in Java?
- In how many ways we can concatenate Strings in Java?
- How many ways are there to initialize a final variable in java?

Advertisements