Solved

Why do I need to handle or declare exception in the finalize method?

Posted on 2011-03-10
5
322 Views
Last Modified: 2012-05-11
I need help with Garbage Collection  in Java. I have 2 questions regarding finalize method:

Why do I need to handle or declare exception in the finalize method in line 12?
(When I replace super.finalize(), the code works fine)

I know finalize method should  execute at least once when the Garbage Collection starts working, so I compiled and run the code several times to see if I could find the finalize method prints “Hello” or “World” in the output (because I was hoping the Objects will be Garbage Collected). Why is it not producing any output after handling the exception? Let me know if I got the finalize and Garbage Collection concept right.

public class FinalizeTest {
    private String value;
    public FinalizeTest(String text) {
        this.value = text;
    }
    public static void main(String[] args) {
        FinalizeTest f1 = new FinalizeTest("Hello");
        FinalizeTest f2 = new FinalizeTest("World");
        f1 = null;
        f2 = null;
    }
    public void finalize(){ 
        System.out.println(this.value);
        super.finalize();
    }
} 

Open in new window

0
Comment
Question by:techbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35099390

This explains why it is a good practice to have a try/catch loop when overriding finalize:
http://www.janeg.ca/scjp/gc/finalize.html

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 35099599


This works and you'll see the output:

public class FinalizeTest {
    private String value;
    public FinalizeTest(String text) {
        this.value = text;
    }
    public static void main(String[] args) {
        FinalizeTest f1 = new FinalizeTest("Hello");
        FinalizeTest f2 = new FinalizeTest("World");
        f1 = null;
        f2 = null;
        System.gc();
    }
    public void finalize() throws Throwable {
        try{
            System.out.println("ttt");
        System.out.println(this.value);
        }  catch(Exception ex){
            System.out.println("error " + ex.toString());
        }
        finally {
        }
        super.finalize();
        }  
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35099614
This is the output that I see:

ttt
World
ttt
Hello

Open in new window


Otherwise (without System.gc()), perhpas we don't know when
GC would execute, especially if you are running it in IDE which, I guess, maintains
its own VM
 
0
 
LVL 10

Expert Comment

by:gordon_vt02
ID: 35109616
Just to clarify, it is not guaranteed that your finalize method will ever be called by the JVM.  There are very rare instances where you actually need to use them and it is good practice to avoid them if at all possible.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question