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!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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  RSS feed

 
Greenhorn
Posts: 3
  • 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?
 
Ranch Hand
Posts: 144
Java Linux Netbeans IDE
  • 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
  • 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
  • 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?

 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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?
 
Greenhorn
Posts: 4
  • 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
 
Ranch Hand
Posts: 808
  • 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.
 
Ranch Hand
Posts: 50
Android Java
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!