aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help,A GC problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help,A GC problem" Watch "Help,A GC problem" New topic
Author

Help,A GC problem

la fyuan
Greenhorn

Joined: Oct 02, 2011
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

Joined: May 29, 2011
Posts: 144

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


4
1


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
la fyuan
Greenhorn

Joined: Oct 02, 2011
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

Joined: May 29, 2011
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

Joined: Jan 28, 2008
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

Joined: May 29, 2011
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

Joined: Jan 28, 2008
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

Joined: Oct 02, 2011
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

Joined: May 29, 2011
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

Joined: Jan 28, 2008
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

Joined: May 29, 2011
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?
Puneet Chadha
Greenhorn

Joined: Oct 03, 2011
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

Joined: Mar 12, 2011
Posts: 808
Seems like there are Dozens of threads about this problem.

I like this post from Ikpefua for the diagram he shares. The best way to get a handle on GC is to draw a picture.
John Schubert
Ranch Hand

Joined: Sep 21, 2011
Posts: 39

5 Objects created (2 Dozens with their respective int arrays and the Dozens array named da).
1 Object collectable (the one previously referenced by d and da[1])

Hoping that in the exam i'll be provided with something to draw.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help,A GC problem