GC of Method Local String and Instance Local String

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
PMiglani
Posts: 14
Joined: Sat Jan 19, 2013 8:42 am
Contact:

GC of Method Local String and Instance Local String

Post by PMiglani »

Hi,

I have confusion about following two cases

How many objects will be eligible for GC just after the method returns?

public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}


When does the String object “ hi ” instantiated on line 2 become eligible for garbage collection?
public class Hello {
String greeting = “hi”;
public static void main(String [] args) {
Hello h = new Hello();
h.greeting = null;
System.gc();
return;
}
}


The answer to first is
"abc" is a string literal which goes to the string pool and is not GCed.

I want to clarify whether the same holds for second also. Will the local string and instance member string both not be GCEed?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: GC of Method Local String and Instance Local String

Post by admin »

Yes, neither of them will be GCed.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 201 guests