Garbage collection and Finalize() method in Java
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
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:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
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.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
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)
Comments