• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain the result for this Q please?.
Here is a method which creates a number of String objects in the course of printing a
series of messages.
1)public void SoundOff() {
2) for(int i = 0; i < 10; i++) {
3) String tmp = Integer.toString(i);
4) System.out.print(tmp + ",");
5) }
6) System.out.println("10");
7)}
When the program reaches line 6,how many of the String objects created in line 3 are eligible for garbage collection?Assume that
System.out object is not keeping a reference.
a) none
b) 1
c) 8
d) 9
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, d (9)is the correct answer. If it is then, let me know if you need any explanation.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepa,
When you post a qstn, please don't forget to give your part of the explanations. This will make the discussion more interesting than just a question and answer type. And others will know what exactly you want also.
regds
maha anna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)public void SoundOff() {
2) for(int i = 0; i < 10; i++) {
3) String tmp = Integer.toString(i);
4) System.out.print(tmp + ",");
5) }
6) System.out.println("10");
7)}
When i = 0, tmp is created.
i = 1, tmp created above is eligible for GC
i = 2, same as above

similary upto i = 9, same as above.

at the line 5 when i = 9, we have 9 object created. 8 for GC.

at the line 6, the last tmp object goes out of for loop scope, hence it is eligible for GC at line 6.
So my answer is 9.
Is this right?
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepa,
In your sample code, the 'for' loop iterates 10 times (0...9)
So we are creating 10 objects totally inside the 'for' loop. Everytime for a new iteration , the object originally (in the previous iteration) created and help by ref 'tmp' is cut off and since there are NO OTHER references refereing to this object help by 'tmp' ref var, the object in the heap is eligible fo GC.
So having said that, When the loop gets finished, 10 out of 10 objects (All objects created inside the for loop) are eligible for GC. Why? Because the variable 'tmp' is 'out of scope' after the for loop. 'tmp' ref is a local var inside the for loop. According to JLS when a object held by a ref goes out of scope and there are no more ref refering to the object in the program then it is eligible for GC.
This qstn became very popular. Brogden says it is only 9 objects eligible for GC since the last tmp ref is still holding the last created object. He tested in various JVMs and finally concludes, it is JVM dependent. But according to JLS ALL objects created inside the for loop are ELIGIBLE for GC.
regds
maha anna
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here is the explanation from the author of the qstn itself. I searched in Marcus's discusison group and grabbed it for you. Since Marcus's forum has got frames I can't directly take you there.
Refer to this link also from Brogden's official explanation.
regds
maha anna
From Bill Brogden
-----------------
This has really been an interesting Thread alright, I had some guys from Sun playing around with it. If you write a class that has a finalize method that does a System.out.println() you can play with it too. Or, write me and I will send an example.
What I found was that the JDK 1.02 level compiler and runtime did NOT gc the object that was out of scope. The JDK 1.2.2 compiler and runtime DO gc the object that is out of scope. AND - even more amazing, appear to gc objects that are IN scope but are not referred to later in the method.
Altogether a very interesting investigation.
Be glad there is nothing as weird as this on the test.
 
Deepa sivasankar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha anna,
Thanks a lot for your detailed explanation. I have one more clarification about the foll Q.

How many objects are created in the following?
String A,B,C;
A="1234";
B=A;
C=A+B;
a. 1
b. 2
c. 3
d. 4

I saw the answer is 1 , But can anyone explain how it is 1?.
How can we assume the string "1234" is assumed to be present in the string pool?.
The answer should be 2 right???. "1234" is created ( i.e. if a there is an other reference to "1234" then a new object is not created because it is in the string pool) and C= B+A creates one more object so totally 2. right.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Often an offshoot of this discussion is a deliberation about objects in the string pool. Some questions are made very clear by stating one can assume statements like println() do not keep a reference. But others are not worded clearly which causes ambiguity and confusion.
I guess, for the sake of the exam, it is safe to assume not to worry about strings in the string pool. Because it is not one of the objectives to learn how the objects in the string pool garbage collected.
Do you folks agree ??

Ajith

[This message has been edited by Ajith Kallambella (edited May 10, 2000).]
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the Exam you assume that all the GC qstns are with objects created in the heap.
For your doubt, In your qstn whether "1234" we can't assume that there is already a "1234" object in the String pool. But maximum, we can be sure this "1234" object will be there in the String pool. So since we can't assume there is pre-existing "1234" object in the Stirng pool actually 2 objects are created after all the statements executed. "1234" is a String object and we can very well call all the String class methods on it. If the author WOULD HAVA CLEARLY told that there is already a "1234" in the String pool then we can confidently say there is ONLY ONE object created.
regds
maha anna
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree that you don't have to worry to much about the String pool.
The GC questions are mostly theory in nature. The only thing in terms of a practical question may be, for example, when a certain reference is eligible for GC or something like that. But the questions on the exam are not cryptic like the question being discussed in this thread.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic