Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

where is it possible that garbage collector...

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q) where is it possible that the garbage collector will run the first time?
---------------

1: public void methodGC(String s){
2: String a, b;
3:
4: a = new String("GC");
5: b = new String("Test");
6:
7: System.out.println(a+b);
8:
9: a = null;
10: a = b;
11:
12: System.out.println(a+b);
13: }
a: just after line 7
b: just after line 9
c: just after line 10
d: just after line 11
I think the answer is a(line 7).
Is this right?
Thanks for your help!
[ March 13, 2003: Message edited by: K.S Moon ]
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I m relly not able to get y ,after line 7.
but there is chance of gc to run after line 9
regards
Praful
[ March 13, 2003: Message edited by: Praful Thakare ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, the correct answers are A and B because on line 7 a new String object is created due to the a+b expression (). Since the resulting String is not assigned to any reference it becomes eligible for garbage collection just after line 7.
Answer B speaks for itself since the reference variable a is assigned null, and thus, the String object that was previously referred to by a is now unreferenced, i.e. eligible for garbage collection.
As a side note, it is worth noting that if the refrence variables a and b referred to String literals as in
a = "GC";
b = "Test";
then the expression a+b would be another String literal, namely "GCTest" resolved at compile time, and thus, not garbage collectible.
IMPORTANT: HOWEVER, for the purpose of the exam, you should not make the difference between String literals and String objects, you should treat them as being both garbage collectible, and you should not assume the existence of the String pool.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is possiblity for GC at line 7, because in System.out.println() statement when we will use '+' operator, one more (resulted) String object will be created. This object will not have any reference, due to this it is eligable for GC.
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tausif and Valentin
U guys r absolutely right only thing is
when System.out.println(a+b) is called it will create and new object of StringBuffer instead of String.
Valentin even if a and b points to String literals like a="some" ,b="another" and v say
System.out.println(a+b) the working of System.out.println will be same as above i.e another StrinBuffer object will be created.
Moral of the story is gc will be executed first after line 7
regards
Praful
[ March 13, 2003: Message edited by: Praful Thakare ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only thing is when System.out.println(a+b) is called it will create and new object of StringBuffer instead of String.
Actually, the use of a StringBuffer to perform the concatenation is not mandatory, but it is an implementation-dependent optimization (JLS 15.18.1.2 Optimization of String Concatenation). For the purpose of the exam, you should not assume that a StringBuffer is behind this concatenation.
Valentin even if a and b points to String literals like a="some" ,b="another" and v say
System.out.println(a+b) the working of System.out.println will be same as above i.e another StrinBuffer object will be created.

I'm sorry I forgot to add something very important. The reference a and b must also be declared final. If a and b referred to String literals and were final, the expression a+b would be resolved at compile-time (JLS 3.10.5 String Literals), and thus, there would be no object eligible for garbage collection after line 7 during runtime.
That is,
final String a = "GC";
final String b = "Test";
System.out.println(a+b);
would be transformed by the compiler to
System.out.println("GCTest");
and you can see that there is no concatenation anymore.
To prove this, let's decompile (by using javap) the following code and look for the different String literals in the bytecode.

After running javap, we get

As you can see the concatenation has been resolved at compile-time. Sorry for the disgression, but I thought this is an interesting topic.
Moral of the story is gc will be executed first after line 7
For the purpose of the exam, this is correct
[ March 14, 2003: Message edited by: Valentin Crettaz ]
 
Praful Thakare
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,
thanx.that was relly good knowledge++ 4 me
regards
.Praful
 
K.S Moon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tausif and Valentin
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where is it possible that the garbage collector will run the first time?
Maybe I'm not getting this but isn't the correct answer, anytime and anywhere? The garbage collector runs when it decides that it needs to run, not when something goes out of scope.
By the way, for this simple example I doubt that GC would ever run. Unless there was something else running in the JVM chewing up resources, why would GC even bother running to release a String?
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Paul. But the explanantion given by others really helped me. Thanks every one.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas is correct...
The exam will use very specific language like:
'When will an object be eligible for garbage collection?'
We read a lot of posts from people who did really well on the exam but still missed a few garbage collection questions...
One guess I have is that the garbage collection questions use tricky language, because garbage collection itself is one of the easier topics.
So, be VERY careful about the wording of the G.C. questions.
For instance, there might be a code listing, and you can tell that after line 12 an object is ELIGIBLE for G.C., all of the following answers will be FALSE:
1 - After line 12 the object will be G.C.ed
2 - The G.C. will run after line 12
3 - After line 12 the G.C. is guaranteed to run.
The key point is that the ONLY thing we can know for sure is that a given object is ELIGIBLE for G.C. There are no other guarantees!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic