garbage collection question
Question 1
object o = null;
for(int i=0;i<5;i++)
{
o = new object();
}
Q how many objects are avaliable to be collected.
Ans. 4
because object o still holds a reference when it exit from the for loop
Question 2
object o;
for(int i=0;i<5;i++)
{
o = new object();
}
Ans: 5
why?
what's different between object o and object o = null;
I am confused, can anybody give me a hint.
thx in advance
Steven
one more question, I understand that every thing declare using "new" will be garbage collected when it is not referenced, but what about primitive types? How does JVM handle them. If I force a garbage collection action with System.gc, will JVM starts gc immediately. Plz help
I am not sure, bt my guess would be that the declaraton Object o generates a reference to a piece of memory, which is unitialized, that gets freed the first time through the loop.Object o = null doesn't, since the reference is null.
The gc runs when it wants to, you cannop force it. Calling System.gc() really doesn't do anything, it's only a suggestion, so don't bother.
Hi,This is the answer to your secod question :Garbage Collection can never be Forced. By telling System.gc(), you are telling GC could be run, but the timing is decided by JVM. It only gives you more probability for GC to run immediately.Rajesh
I do not know ans for the first question ...
but the second one would work like this ...
Objects are created on heap hence needs to be Garbage collected.
Primitive data types are created on Stack and hence will be lost after it comes out of the scope of function or class(if it is class variable)
and for System.gc does not run garbage collector immediately.. we cannot force garbage collector to run..
it runs as a low priority thread.
I see what you meant. I think any other possible reason, maybe you are right, But my stupid book keep telling me reference will not declare a piece of memory unless you assign one using new key word.
Does JVM g-collector need to worry about primitive objects. My guess is that the primitive type object will be push and pop instead of been collected. Any comments.
Thx you all for answering my question.
> Question 1
> object o = null;
> for(int i=0;i<5;i++)
> {
> o = new object();
> }
> Q how many objects are avaliable to be collected.
> Ans. 4
This is correct.
> because object o still holds a reference when it exit
> from the for loop
>
> Question 2
>
> object o;
> for(int i=0;i<5;i++)
> {
> o = new object();
> }
> Ans: 5
The answer is not 5. It is 4 just like in the previous case. However I think you meant "Object" not "object". In the second case if the variable declaration were inside the for loop, then the answer would be 5.
> why?
> what's different between object o and object o =
> null;
In the first case o is uninitialized, in the second you set it to null. However since you immediatly asign it a new value the code in both cases is equivilent.
> I am confused, can anybody give me a hint.
> thx in advance
>
> Steven
daney at 2007-7-4 13:19:11 >
