Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

que on GC

 
nisha chidella
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nisha chidella
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Manikanta Mogalluri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


__._,_.___
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no line.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic