This week's book giveaway is in the General Computing forum.We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# Help,A GC problem

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?

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?

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

Ranch Hand
Posts: 808