aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects are Garbage collected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects are Garbage collected?" Watch "How many objects are Garbage collected?" New topic
Author

How many objects are Garbage collected?

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
In the below code after line number 5, how many objects are ready for Garvage collection? Is it 9 or 8. In one of the mock exams, answer is 8. But i am having a doubt, that in for loop we are creating 9 string objectes and all are ready for garbage collection at line number 5, now how is the answer 8.

1 public void method(){
2for(int i =1;i<10;i++){
3String str=Integer.toString(i);
4}
5System.out.println("out of for loop");
6 }


Narasimha
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
I'm trying to help here with a question: How many iteration of the loop are done?


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Since the 'str' string is defined inside the loop then this means it is not available outside it, therefore all the references assigned inside the loop are lost when the loop exits (thus making all the objects inside EFGC). So the answer depends totally on the number of iteration the loop can do (As Thomas suggested), in which case the answer is 9.
However if the 'str' string was defined as a member variable then the last reference of it is not lost, so the answer would be 8.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
Hi!
The string is created in the loop as follows:
loop from 1 to 9.So it creates
str="1" Object1
str="2" Object 2 (Object 1 collected)
str="3" Object 3 (Object 2 collected)
str="4" Object 4 (Object 3 collected)
str="5" Object 5 (Object 4 collected)
str="6" Object 6 (Object 5 collected)
str="7" Object 7 (Object 6 collected)
str="8" Object 8 (Object 7 collected)
str="9" Object 9 (Object 8 collected)
So the answer is 8.
Hi!
I have a question related to this.I read somewhere that the string literals are not Garbage collected.The above is a string literal rite?
The above is not as str=new String("value")-If this is the case, I accept that the value is 8.Can anybody explain pl!Thanks


Software_guy
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Because of the vaguaries of the String constant pool, the real exam won't use String objects when testing GC questions.
The exam WILL test you on String immutability!
The exam WILL test you on knowing how many non-String objects might be eligilbe for the GC.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
How abt the answer for the above question?Anyway, it's using string literal rite?I want to know that the above question is rite r not please?Thanks
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Sridhar Srinivasan:
Hi!
The string is created in the loop as follows:
loop from 1 to 9.So it creates
str="1" Object1
str="2" Object 2 (Object 1 collected)
str="3" Object 3 (Object 2 collected)
str="4" Object 4 (Object 3 collected)
str="5" Object 5 (Object 4 collected)
str="6" Object 6 (Object 5 collected)
str="7" Object 7 (Object 6 collected)
str="8" Object 8 (Object 7 collected)
str="9" Object 9 (Object 8 collected)
So the answer is 8.
Hi!
I have a question related to this.I read somewhere that the string literals are not Garbage collected.The above is a string literal rite?
The above is not as str=new String("value")-If this is the case, I accept that the value is 8.Can anybody explain pl!Thanks

I have my doubts about this.
Object 9 goes out of scope the moment the loop terminates so becomes elligable for garbage collection at that time.
That would mean 9 objects to be garbage collected.
But indeed as there are 9 String litterals there's nothing here. String litterals aren't garbage collected (wasn't that changed though recently? or not in the 1.4 spec) so there are actually 0 Objects elligable for garbage collection???.


42
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
But still outside the loop, str refers to Object 9 rite?Then how come it goes out of scope?
I too thought that the object collected should be 0 since str refers to string literal.
Can anybody explain pl!Thanks
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The 'str' reference is defined inside the for-loop therefore this reference won't be available outside it, so any object that 'str' refers to will EGC as soon as the loop exits.
As for your question, there are 9 object EGC. You should always take "Bert Bates" consideration in terms of the eligibility of strings for garbage collection.
Haven't even read my first post!!!?
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
public class A{
public void method(){
String str = "TEST"; // 1
}
public void onemoremthod(){
String str1 = "TEST"; // 2
}
}
HI,
In the above programme, when i create a string object at line number 2, will create a new string in the pool or will it refer the same as created in the line number 1. I think at line number 2, no new object will be created, simply str1 will point to the string object created on line 1. And string and wrapper classes are immutable, hence these objects will not be garbage collected.
I am not sure, above undestanding of mine is correct or not. I hope seniours can correct.
Thanks in Advance,
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
narasimha , You are correct, both 'str' and 'str1' will refer to the same �TEST� object, check this code.

Strings and Wrapper classes are immutable, but what makes you think that these object are not eligible for garbage collection!!! These objects like any other objects will be eligible for garbage collection as soon as they lose their reference.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5

Strings have a special place called the String literal pool, they aren't treated like other objects in terms of GC, I swear I'm not kidding on this :roll:
Vicken -
In your code, if s1 == s2 then we know that we have two reference variables referring to the same object, right? This object is in the String constant pool, not the heap where other objects live. The GC is only concerned with the heap. I believe that certain VMs may handle the String constant pool differently, and you might be able to deduce rules for a particular VM, but for the exam - the GC's relationship to Strings is not tested.
[ February 12, 2004: Message edited by: Bert Bates ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Yes, Bert you are correct, I am backing you 100% as my 2nd post suggests, but for the sake of simplicity my previous post was not that accurate.
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
I don't think we can ever get an answer from Bert regarding String GC. Someone posted similar question using string for GC before and I did in fact ask Bert for an answer to the guestion. Unfortunately, Bert advised me to change the string to other type then only answer will be given.
So I guess we could just take Bert's adivce and move on from here.
IMHO, the correct answer (if not string type) would be 9.
Cheers.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
CH -
You're right, but I'm not doing it to be cranky
I've made it my personal mission to try to keep the threads in this forum focused on the SCJP exam, I know, radical, but I'm thinking "What the heck, it IS a cert forum after all".
So, if I ever wandered over to JIG intermediate or advanced, and someone were to ask about the GC's relationship to Strings, I might have to jump in, but not in this forum!
aloha,
Bert
(the focus police)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects are Garbage collected?