| Author |
que on GC
|
nisha chidella
Ranch Hand
Joined: Jul 24, 2008
Posts: 57
|
|
source: MasterExam
while the code represented by //do time consuming stuff is executing, which are true concerning three Phoenix objects created.
the ans given is : one
why ? aren't both objects created at line 1 and 2 eligible ? why only the first ?
|
SCJP 5.0(100%)
Preparing for SCWCD
|
 |
Ken Truitt
Ranch Hand
Joined: Aug 23, 2007
Posts: 124
|
|
The first instance of Phoenix gets its only reference variable when it's assigned to p2. It loses it in the next
statement when the next instance of Phoenix is itself assigned to p2--the same static variable in History.
So the second instance of Phoenix remains linked to the program by p2, while the first instance has no
reference and is thus eligable for GC.
And I think the p3 instance isn't on the heap because it's in a method.
|
SCJP 88% | SCWCD 84%
|
 |
nisha chidella
Ranch Hand
Joined: Jul 24, 2008
Posts: 57
|
|
Thanks Ken....i didn't pay attention that p2 is static.
about the third object, ofcourse it's on the heap, but it has a reference p3.
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
nisha chidella wrote:
about the third object, ofcourse it's on the heap, but it has a reference p3.
Yes , its an object and located on heap, but it will only considered for GC, when the main method get exits and the object has no longer a valid reference. See, the last and third object is local to main method, so its reference is on main method stack , but object get created on the heap ..
I hope this help you !!
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Manikanta Mogalluri
Greenhorn
Joined: Dec 31, 2008
Posts: 11
|
|
Dear All,
Could any body explain the answer for the following question
Which is the earliest line in the following code after which the object created in the line marked (0) will be a candidate for garbage collection, 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; // (2)
b = a; // (3)
d = a; // (4)
return c; // (5)
}
public static void main(String[ ] args) {
String msg = f();
System.out.println( msg); // (6)
}
}
With Warm Regards,
Manikanta Mogalluri,
+91 9941539542.
__._,_.___
|
With warm regards, Manikanta Mogalluri
SCJP(90%), SCWCD(100%)
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
no line.
|
SCJP 6
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Welcome to JavaRanch,
Here, the all the object are string literals defied and hence created and referenced from String Constant Pool, and this pool get destroyed only when JVM exits !!
So the answer suggested by Punit is right, no line !!
HTH,
|
 |
 |
|
|
subject: que on GC
|
|
|