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

[3439 byte] By [mipsmea] at [2007-11-27 3:11:12]
# 1

The garbage collector doesn't run immediately after an object becomes garbage. It only runs when your heap fills up and it needs to reclaim space for further object allocations. (This is the main concept -- the algorithms are much more sohphisticated, the exact policy depends on your selected garbage collector and its parameterization, the sizing of your generations and so on...)

For example, if you would add a loop to your program and create some large objects and throw them away again, this would trigger the garbage collector after a while.

Also see http://java.sun.com/javase/technologies/hotspot/gc/index.jsp, there's a good whitepaper also.

Nick.

nicolasmichaela at 2007-7-12 8:13:38 > top of Java-index,Desktop,Runtime Environment...
# 2

// this would trigger the garbage collector after a while.

nicolasmichael, yes that's right it doesn't run immediately but I did the same thing but with an array of 10,000 Objects

I waited for about 15 minutes and the gc didnt work during this period which makes sense !!!!!!!

Message was edited by:

mipsme

Message was edited by:

mipsme

Message was edited by:

mipsme

mipsmea at 2007-7-12 8:13:38 > top of Java-index,Desktop,Runtime Environment...
# 3

The (young) GC runs only if your (eden) heap fills up. If you stop creating objects before your (eden) heap is full, you can wait forever -- the GC will never run.

Try to create object arrays in an infinite loop and run "jstat -gc <pid> 10000" in parallel (jstat is part of the JDK distribution as of 5.0) and watch the columns EU ("eden usage") and EC ("eden capacity"). Once EU reaches EC, a young collection will be triggered (you will see an increase in YGC).

Nick.

nicolasmichaela at 2007-7-12 8:13:38 > top of Java-index,Desktop,Runtime Environment...