Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Help,A GC problem

la fyuan
Greenhorn
Posts: 3
Given:
public class Eggs {

public static void main(String[] args) {

Dozens[] da=new Dozens[3];
da[0]=new Dozens();
Dozens d=new Dozens();
da[1]=d;
d=null;
da[1]=null;
}

}

class Dozens{
int []dz={1,2,3,4,5};
}

Question:
How many objects were created?
How many object(objects) is(are) eligible for GC?

Hama Kamal
Ranch Hand
Posts: 144
Question:
How many objects were created?
How many object(objects) is(are) eligible for GC?

4
1

la fyuan
Greenhorn
Posts: 3
Hama Kamal wrote:
Question:
How many objects were created?
How many object(objects) is(are) eligible for GC?

4
1

More specific?

Hama Kamal
Ranch Hand
Posts: 144
la fyuan wrote:Given:

Question:
How many objects were created?
How many object(objects) is(are) eligible for GC?

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Well, Actually there are 5 objects created, and only 1 object is eligible for GC.

Note* but end of the method , all the above 5 objects will be Garbage Collected.

Hama Kamal
Ranch Hand
Posts: 144
Seetharaman Venkatasamy wrote:Well, Actually there are 5 objects created, and only 1 object is eligible for GC.

Note* but end of the method , all the above 5 objects will be Garbage Collected.

are you sure that at line 2 two objects are created?

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Hama Kamal wrote:are you sure that at line 2 two objects are created?

array int[] is an object. when Dozens object get initialized, it initialized an object []dz of type int array. what do you think?

la fyuan
Greenhorn
Posts: 3
Seetharaman Venkatasamy wrote:Well, Actually there are 5 objects created, and only 1 object is eligible for GC.

Note* but end of the method , all the above 5 objects will be Garbage Collected.

Thank you. The answer to this question is right!

Hama Kamal
Ranch Hand
Posts: 144
Seetharaman Venkatasamy wrote:
Hama Kamal wrote:are you sure that at line 2 two objects are created?

array int[] is an object. when Dozens object get initialized, it initialized an object []dz of type int array. what do you think?

well i think there is only one Object and it's referenced from da[0],,,, do you mean that da[0] refers to two objects?

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Hama Kamal wrote:do you mean that da[0] refers to two objects?

It is a chaining, da[0] referes Dozens object, which internally referes []dz object. clear now?

Hama Kamal
Ranch Hand
Posts: 144
Seetharaman Venkatasamy wrote:
Hama Kamal wrote:do you mean that da[0] refers to two objects?

It is a chaining, da[0] referes Dozens object, which internally referes []dz object. clear now?

Seetharaman what you say is quite reasonable and logical but i want to ask if it is your own conclusion or you got if from a text book?

Greenhorn
Posts: 4
2 objects will be eligible for garbage collection
the one originally reference by d or da[1] & the other one being the int array reference d.dz or da[1].dz

dennis deems
Ranch Hand
Posts: 808