• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help,A GC problem

 
la fyuan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:
How many objects were created?
How many object(objects) is(are) eligible for GC?


4
1

 
la fyuan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic