aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "GC " Watch "GC " New topic
Author

GC

shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Hi,
1. public void countDown() {
2. for( int i = 10 ; i >= 0 ; i-- ){
3. String tmp = Integer.toString( i );
4. System.out.println( tmp );
5. }
6. System.out.println("BOOM!");
7. }

At line 6 , how may objects are eligible for garbage collection?
I chose 1 for tmp, but the answer says 10. Even if you include each of the Integer objects, the answer should be 11+1=12.
11 since the loop iterates 11 times.

------------------
Regards,
Shree


Regards,<BR>Shree
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
Before line 6 it should be 11 at 6 should be 12


I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
Hi,
What about tmp. Can you elaborate?
------------------
Regards,
Shree
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
Vijay I think only 11 will be eligible for GC.tmp wil hold one string
Ash Rai
Ranch Hand

Joined: Dec 15, 2000
Posts: 34
Shree,
The loop iterates 11 times and the 11th time String tmp holds Integer.toString(0). So there are 10 objects(Integer.toString(10).... Integer.to String(1)) which are eligible for GC.
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
what about "BOOM!"
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
The string tmp is declared within the for loop. At line 6 the for loop has ended and therefore, tmp is no longer in use. Doesn't that mean it is eligible for GC? Also, what happens when a method is quit -- is the memory for the local variables released automatically or they wait for the GC to free them?
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
i agree with Ash,
as it is asked number of objects eligible for GC at line 6, so the answer would b 10, as even though temp is out of scope in line 6, the local variable still has reference.
Hemant Patel
Greenhorn

Joined: Dec 15, 2000
Posts: 20
hi
Shree
here tmp is declared in for loop so when loop iterates 11 time then that eleven. so their should be 11 object eligible for garbage after for loops block over (at line 6)
Himanshu Amin

------------------
"JAVA BANAYE BAVA"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC
 
Similar Threads
One more question on garbage collection
garbage collection
gc
Variables out of scope eligible for garbage collection
How many are eligible for GC