By: aathishankaran in Java Tutorials on 2007年03月05日 [フレーム]
Garbage collection in Java is the process of freeing up memory that is no longer in use by the program. In Java, garbage collection is done automatically by the JVM. When an object is no longer reachable, it becomes eligible for garbage collection.
The finalize() method is a method that is called by the garbage collector just before an object is about to be garbage collected. The purpose of the finalize() method is to give an object a chance to clean up any resources it has allocated before it is destroyed.
Here's an example of how the finalize() method works:
public class Box {
private int width;
private int height;
public public Box(int width, int height) {
this.width = width;
this.height = height;
}
@Override
protected void finalize() throws Throwable {
System.out.println("Box object is being garbage collected.");
}
}
public class Main {
public static void main(String[] args) {
Box box = new Box(10, 20);
box = null; // make the object eligible for garbage collection
System.gc(); // call the garbage collector
}
}
In this example, we define a Box class with a finalize() method that simply prints a message to the console. In the Main class, we create a Box object and then set the object reference to null, which makes the object eligible for garbage collection. We then call the System.gc() method, which suggests to the JVM that it should run the garbage collector. The garbage collector will then call the finalize() method of the Box object before it is destroyed.
It's worth noting that the finalize() method is called by the garbage collector, which means it's not guaranteed to be called at any particular time. In fact, it's possible for the finalize() method to never be called at all. Therefore, the finalize() method should not be relied upon for critical cleanup tasks. Instead, it should be used as a last resort when other cleanup mechanisms are not available.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Latest Articles (in Java)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate