Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please solve the following Problem.

 
Shishir malviya
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects will be eligible for garbage collection after line 7?

Choices:

A. 0
B. 1
C. 2
D. 3
E. Code does not compile
[ July 02, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us what you think the answer is.
 
Nikhil Jain
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the answer is 3.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only 1 object(new Integer(100)) eligible for Garbage collection

Other 2 have references to it
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only 1 object(new Integer(100)) eligible for Garbage collection
Other 2 have references to it


Should it not be 2 objects ,
1. IntegerObject - a
2. The String used in the constructor - "100" ( ??? )
[ July 02, 2005: Message edited by: Srinivasa Raghavan ]
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agreed with vidya.

about the member who posted above me, i can't see the name right now replying but the member said about shouldn't it be 2 objects be elibgable for garbage collection.

Integer Object will be the one who eligable for GC.

String Object wont be elibgable because it was not set to null.

c=b; // here said c refers to b which c is new variable given to new String.

so the c in the "String c= new String("100"); still not yet elibgable.

correct me if i'm wrong. thanks...

and this question i saw it b4. i bet most of members here do too.

all thebest
[ July 02, 2005: Message edited by: Nicky Eng ]
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I aggry with u Vidya.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Choices:

A. 0
B. 1
C. 2
D. 3
E. Code does not compile

[ July 02, 2005: Message edited by: Barry Gaunt ][/qb]<hr></blockquote>


The answer is 1.
Three objects are created during the execution of the main method and one object is created during class (TutorialGC) load time representing the String literal. At line 7 (after method execution), one of the objects thas has been created (with the new keyword) have no strong references referring to it (note that 'out of scope' does not imply 'not referring to' as is often misunderstood). The other two created objects maintain strong references to them. The object representing the String literal becomes eligible for GC when the object referred to by TutorialGC.class.getClassLoader() becomes eligible (which clearly is not the case on line 7).
[ July 04, 2005: Message edited by: Tony Morris ]
 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is 1, here's my thinking:
Line 1 - 3
a -> Integer
b -> Long
c -> String

Line 4
a = null
(notice here the previous object that a pointed has no reference to it, so Integer(100) is eligible for GC)

Line 5
a = c
reference a points to the object that c is pointing to (ie. String("100"))

Line 6
c = b
reference c points to the object that b is pointing to (ie. Long(100))
notice here the previous c object (String("100")) still has a reference a

Line 7
b = a
reference b points to the object that a is pointing to (ie. String("100"))
previous b (Long(100)) does not lose reference because in Line 6 c pointed to it

so, finally, only the Integer(100) at the very beginning is eligible for GC
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic