gc seems not working as expected !
Hey All
We know that gc frees the memory occupied by an object when no references are there for this object and invoke finalize() method on it.
I tried the following case :
1. I have an object on which i overrided finalize() method to know when the gc finalizing this object.
publicclass AnObject{
/** Creates a new instance of AnObject */
public AnObject(){
}
protectedvoid finalize()throws Throwable{
System.out.println("FINALIZED");
}
}
2. in my Demo class i have instance variable of type AnObject as shown:
publicclass Demoextends JFrame{
private JButton button;
private AnObject obj;
/** Creates a new instance of Demo */
public Demo(){
//
obj =new AnObject();
initGui();
}
privatevoid initGui(){
button =new JButton("Ok");
button.addActionListener(new ButtonListener());
Container container = getContentPane();
container.add(button, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
privateclass ButtonListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent event){
System.out.println("Finalizing...");
obj =null;
//System.gc();
}
}
publicstaticvoid main(String[] args){
new Demo();
}
}
when running this Demo and clicking the button, the object reference is assingned to null. i'm still waiting for FINALIZED to be printed but with no luck which seems as the gc hasn't finalized the object.
but when removing the comment from System.gc which explicitly calls the gc i got the FINALIZED printed on the console.
so what do you think ?
Message was edited by:
mipsme

