File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on GC from JQPlus 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 "Question on GC from JQPlus" Watch "Question on GC from JQPlus" New topic
Author

Question on GC from JQPlus

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
This garbage collectin questin is from the JQPlus with question ID: 965441698130:
How many objects are elegible for garbage collection when the control reaches line 4
public void process ( int count ) { // line 1
for ( int = 1; i < count; i++ ) { // line 2
Object temp = "Hello" + i; // line 3
} // line 4
} // line 5
The explanation to the question says that "had line 3 been Object temp = new Object("Hello" + i);", four objects would have been eligible for garbage collection. And I agree with the statement. According to my understanding, the answer to the question should be that zero object is eligible for garbage collection. However, "4" is marked for correct answer.
Do I have problem with my reading comprehension? Of course, I may be wrong again as usual.

Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
I'm not sure how many object has been created, since I don't know what the count is. Remember, garbage collection works on the object that has been created using new operator only.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3324
    
    8
That looks like a mistake in setting the right answer. The explanation given is correct though...
====================
All the String objects are interned ie. Java caches all the string objects ( except that are created by doing new ). If you try to create the same string again, you'll get the reference to the same object. For eg. String s1 = "hello"; String s2 = "hello"; then s1 == s2 is true.
It never releases the string objects created even if the variable goes off the scope. In actual big projects this has been a serious problem. If you keep creating big strings (for eg. like XML messages), java never releases them and soon the system runs out of memory. So it is advised to use StringBuffer in such cases.
In the above case all the string objects are cached and none is garbage collected.
Note that, had line 3 been, Object temp = new Object(); 4 objects would have been eligible for GC. Because, the loop runs from 1 to <5 ie. 4 so 4 objects will be created. At line 4 all the objects will be eligible for GC. Some people say that only 3 are eligible for GC as the last object is still refered to by 'temp' but the point is temp is declared in the for loop and goes out of scope at line 4. So, the last object is also eligible for GC.
===================
thanks,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on GC from JQPlus