Author Topic:   Strings - Khalid
deneb shah
greenhorn
posted April 05, 2000 10:12 PM             
Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?

public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
choice sgiven were:
after line 1
after line 2
after line 3
after line 4
after line 5


My answer:
The line marked 0 is making a string with "Bye" so it will be in the constant pool. Now the objects in the pool arent garbage collected because
1 they are not created with new
2 they are available throughout the program

so according to me it should be 5 i.e. the end of the program.
but i got this one wrong ?

my fundas have been uprooted....

SOS SOS SOS

------------------
denice the menace

John Wetherbie
ranch hand
posted April 06, 2000 08:58 AM             
Do you know what the answer is supposed to be according to the mock exam?

I believe the answer is after line 3.

Here is the sequence (I think :-))

At line 0 you have created a String object (bye) that is referenced by b.

At line 1 you create a new String object (bye!) that is referenced by c.

You then have a reference d which is an alias of b since they reference the same object.

line 2 then has b switch from referencing bye to referencing hello. d still references bye. (You changed what b referenced, not the object that b was and d still references.)

line 3 has d reference hello. There are no references to bye now.

The use of String x = "string stuff"; is just shorthand. You have created an object. Having the function be static does not affect b being eligible for garbage collection once there is nothing referencing it.

Jim Yingst
sheriff
posted April 06, 2000 02:06 PM             
Ms. Menace - So does that mean that you are now unfunded?

You are technically correct. "bye" is in the String pool and won't be collected. The thing is: a) in the real exam, you won't be asked about collection of strings which are in the intern pool, and b) in mock exams, often the authors are unaware of how the string intern pool works, and they inadvertantly ask questions whose correct answer depends on the intern pool. So the easiest approach is: for certification purposes, pretend the intern pool does not exist. In this case, after line 3 is correct, as John explained.

John Wetherbie
ranch hand
posted April 06, 2000 03:46 PM             
Jim - can you point to somewhere online (or in a book) where the operation of this pool is explained? I read the JLS section regarding String literals and the intern pool but could not determine from this how things would work when there were no references in the JVM referencing a particular string in the pool. Does it stay around until the program ends or the pool fills up or does the String class realize no one is using it and get rid of it?

deneb shah
greenhorn
posted April 06, 2000 08:33 PM             
my thanx to John and Jim....
for their sincere replies.... feel relaxed
My exams on the 12th April...
so was just confused as to what was happening...

whatever be my score, its this ranch that has boosted me ( with people like Jim, Maha, Tony, and many mor people..) . i especially liked that this forum encourages people to think and not spoon feed....

Congrats to all of us guys n gals ... we are all responsible for this forum....

thats means only 5 days left..

besides its a way to make new friends.... folks my email ID id deneb007@yahoo.com
:cool

------------------
denice the menace

Jim Yingst
sheriff
posted April 06, 2000 11:48 PM             
John- hmmm, don't know a good source offhand. Most of my info came from careful reading of the JLS and the JVM spec, reading between the lines and discussing with others. For extra fun lately a few of us have been looking at some of the jdk source code to figure out what it really does - that's how it was discovered that pooled strings can be GC'd if there really are no references to them - but that's rarer than you may think. One key point is that each class has its own table of constants which is separate from the intern pool (of which there is only one in the whole JVM). When a class is loaded, the constant table is created, and for a String this holds a reference to the String object which is created as part of the intern pool. Which means that as long as any classes which mention that particular string literal are still loaded in the JVM, there will be at least one reference to the interned string. The JVM can't GC the string because it never knows when the class code that uses the literal might be run again - that's too complicated for the JVM to figure out.

Deneb/Denice- glad we can help. It looks like I'm going to be pretty busy and out of town most of next week, so I'd probably better wish you luck now, as I may well be missing from these forums for a while.

Ihor Strutynskyj
greenhorn
posted April 07, 2000 01:20 PM             
I would continue Jim's reply.
The garbage collections of intern strings is related to dynamic class unloading. I saw some on this site that talk about it.

satya5
ranch hand
posted April 07, 2000 11:04 PM             
Jim/John:

In my openion, when 'String d' is declared
between lines 1 and 2, we create another
object and refer to the literal in the intern
pool. So I think after line 2, the reference
to object b is lost and it is ready for
GC. Since object d refers the literal in the
intern pool and not the object b.

My choice would be 'after line 2'
Am I missing something ?

- satya


[This message has been edited by satya5 (edited April 07, 2000).]

[This message has been edited by satya5 (edited April 07, 2000).]

maha anna
bartender
posted April 07, 2000 11:27 PM             
Satya,
You are partly correct. See the line above b=a; statement.

String d = b;
b = a; // (2)

Before ref b is assigned to anther object which is referenced by ref a, one more ref of type String is assigned with ref b by String d=b;. So eventhough b is made to refer to another object, there is still one more ref d pointing to the object which was originally ref.d by b. So in order to make the object eligible for GC d also has to set to null or made to refer to some other object which is done as in the last line of the above code segment d=a; So after this statement only the object "bye" is eligible for GC.
regds
maha anna

satya5
ranch hand
posted April 08, 2000 12:13 AM             
Maha Anna:

As I understand, I am not GC'ing the object "bye".
"bye" is a literal in the internal pool and b is
another object refering that literal. Similarly,
d is another object refering to the literal.

Hence I am of the view that 'b' can be garbage
collected after line 2.

One other reason I believe this is:

MindQ ques #36

How many objects are eligible for GC at Line A

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
// If I put a println(newestName); here, Frieda is printed

Answer is ONE.

As I see, both these questions are similar.
I still am missing something ?

- satya

maha anna
bartender
posted April 08, 2000 07:36 AM             
Satya,
Did you read all the replies for this post, before you post your qstn?. Your qstn is answered already by Jim in this thread. For SCJP2 Exam sake you have to pretend there are no String literals created in qstns related to GC. Assume that instead of String b= "bye"; it is created in the heap as String b= new String("bye");. Now you go from there. And also I see here you use little loose language. Atleast in this case, the ref 'b' is not GC'd. The object referenced by b is eligible for GC.

Now you read my previous post. You will get it. 2 refs point to SAME object "bye" one is d. Another is b. Both MUST be referenced to another object or null in order to GC the object "bye" which happens at the end of the code segment which is after line marked as 3 .
regds
maha anna

[This message has been edited by maha anna (edited April 08, 2000).]

|