File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A GC Problem in Mock Exam

 
Andrew Cheng
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the earliest point where the memory space allocated to the
string in line 2(s1) can be released.
(A) before line 6
(B) before line 7
(C) before line 8
(D) before line 5
ans:A it will destroy the existing string and create new string
because strings are immutable
The text above is the question,answer and explain.
But I think B is correct answer.
Who is wrong?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't appear that variable s1 gets a new value until *after* line 6:
s1 = null;
The line:
s2+=s1 is the same as s2 = s2 + s1;
This creates a new string intance but assigns it to s2, and s1 still keeps pointing to what it had before.
unless there is a call on line 4 that didn't get copied over, I can't see how answer A is correct. I would say B is correct.

Rob
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is correct ie. before line 6.
initially s1->"Hi"
S2->"There";
because of the statement, s2 +=s1...
which gets resolved to....
s2 = s2 + s1
ie. s2 = "There" + "Hi"
Strings are immutable, so the initial object refered by s2 ie."There" becomes unreferenced. The + operator returns a new object which gets assigned to s2 at line 6.
as a result , s2->"ThereHi"
s1->"Hi"
So initial object refered by s2 has no reference at line 6. Hence the answer.
HIH
Rashmi
[ January 16, 2002: Message edited by: Rashmi Gunjotikar ]
 
Mike Beaty
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string in question would be s1, not s2.
If we were talking about s2, A would be correct.
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhhh ya...the question shuld be about string s2 and not s1, then a is correct.
Sorry!
my mistake, i did't see it properly.
Rashmi
[ January 16, 2002: Message edited by: Rashmi Gunjotikar ]
 
Silver
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think B is right for this question. But if the question is about String s2 declared in line 3, A is correct.
 
Suraj Berwal
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear friends,
i also think b is the right choice. if the source could be known then the question may be rechecked. well i think u should drop a line to the author of the test about this mistake.
keep exploring, have fun
suraj
 
Andrew Cheng
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!I Get it!
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Search the Ranch (by my name) to see a program that shows that String literals are not g.c.
This is so because they are placed in a pool from which there is no known way to release them.
Search the Ranch for string literals.
The conclusions are right if we consider the objects created with new String("...") constructor.
Don't worry, you won't be asked like this in the exam. They only test the reachability of String objects created with the constructor. The don't test String literals reachability.
It just happens that we are that clever in the Ranch
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then what is the correct answer here. because the Strings in question are not created by new operator ?
Originally posted by Jose Botella:
Hi all
Search the Ranch (by my name) to see a program that shows that String literals are not g.c.
This is so because they are placed in a pool from which there is no known way to release them.
Search the Ranch for string literals.
The conclusions are right if we consider the objects created with new String("...") constructor.
Don't worry, you won't be asked like this in the exam. They only test the reachability of String objects created with the constructor. The don't test String literals reachability.
It just happens that we are that clever in the Ranch
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No answer is correct.
The string objects created by string literals will not be g.c.
[ January 17, 2002: Message edited by: Jose Botella ]
 
Andrew Cheng
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Although the question in the mock exam is not very exactly,we learn a lot from it.It is enough,Thanks again.
 
kanuck java guy
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe NO answer is correct, why? Because all the string literal are created not on the heap, but in the "Runtime constant pool!!!"
Also note, the new operator was not used to create the String literal on the heap! So both string literal "Hi" and "There" will exist for the lifetime of the program!
Don't believe me try this (you will see that s1 != s2, but s1 == s3)
public class foo {
public static void main(String[] arg) {
String s1 = "Hello";
String s2 = new String("Hello");
String s3 = "Hello";
System.out.println("s1==s2 : " + (s1==s2));
System.out.println("s1==s3 : " + (s1==s3));
}
}
The key to understanding the program is to notice that not new string in created for s3, rather it is given the SAME reference that string s1 has for the string living in the Runtime constant pool!!!
But for all stupified EXAM purposes you can assume s1, s2 and s3 point to strings created on the heap, since there are so many bad examples out there I guess SUN was willing to overlook this one
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Cheng:

What is the earliest point where the memory space allocated to the
string in line 2(s1) can be released.
(A) before line 6
(B) before line 7
(C) before line 8
(D) before line 5
ans:A it will destroy the existing string and create new string
because strings are immutable
The text above is the question,answer and explain.
But I think B is correct answer.
Who is wrong?


At line 6, there are no longer any references to the string literal "Hi." All strings that previously referenced this literal no longer do. At this point, the string literal object "Hi" is eligible for garbage collection. It doesn't mean it will definately be garbage collected, just eligible. So the answer to the original question is B.
The answer given makes no sense for this question...it's almost like it's an answer for a different question... such as "what is the result of the string assignement on line 5?"
Rob
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic