aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on garbage collection" Watch "doubt on garbage collection" New topic
Author

doubt on garbage collection

Pui Shing Lee
Greenhorn

Joined: Jul 23, 2005
Posts: 10
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4

Is it a) 0?
saran sadaiyappan
Ranch Hand

Joined: Dec 23, 2004
Posts: 39
I suppose it should be zero only as " newestName " has reference to name and so it ll not be eligible for garbage collection.
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

One String object will be eligable for garbage collection, "Nick".


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

One String object will be eligable for garbage collection, "Nick".
A reference to that String object will be retained in the runtime constant pool, as with other String literals.
Rekha Gaikwad
Ranch Hand

Joined: Jul 25, 2005
Posts: 36
One String object will be eligible for collection i.e. "nick"
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

One String object will be eligible for collection i.e. "nick"


A reference to that String object will be retained in the runtime constant pool, as with other String literals. Because of this, the String objects on the heap ("Nick", "Jason", and "Frieda") will *not* be eligible for garbage collection, and the answer should be "a) 0".

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#73272



With this code, you'll have one object eligible for gc at "Line A"; namely, the o1 object, which no longer has any references pointing to it (v2 points to o2, v3 points to o3, and v1 contains the null reference).
[ July 25, 2005: Message edited by: Steve Morrow ]
Pui Shing Lee
Greenhorn

Joined: Jul 23, 2005
Posts: 10
Thx Steve!
Kanchan K Bulbule
Greenhorn

Joined: Jul 19, 2005
Posts: 20
code:
--------------------------------------------------------------------------------

Object v1; Object v2 = new Object(); // <-- o1 v2 = new Object(); // <-- o2 v1 = new Object(); // <-- o3 Object v3 = v1; v1 = null; //Line A

--------------------------------------------------------------------------------





As per your example reference to Nick is lost

" String newName = "Nick";
newName = "Jason"; "

So "Object Nick " is available for Garbage Collection
raval.jignesh raval
Greenhorn

Joined: May 31, 2005
Posts: 2
Hi All
Abt the example reg: Strings for GC I think 0 objects will be GCed since string objects created as follows
String s = "ABC";
s=null;
are never GCed and the string goes in to String pool
but
String s = new String("ABC");
s-null;
now we have one object eligible to be GCed
Please correct me if I am wrong.
Jignesh


Jignesh Raval
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

As per your example reference to Nick is lost

Please read the following: String literals
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
Hi steve,

if article is correct and it seems as i tried ex.

String s="hello";
String b=new String("hello");

then

b=b.intern();

s==b // true

checked others pros and cons s.intern()

etc.

it seems in short article is very true and taught me new concept

but as per article nick is refered from constant pool alright
but there is no way we can access or get to it.
right.
Pan Zhihua
Greenhorn

Joined: Aug 01, 2005
Posts: 9
Originally posted by Kanchan K Bulbule:



As per your example reference to Nick is lost

" String newName = "Nick";
newName = "Jason"; "

So "Object Nick " is available for Garbage Collection


I really agree with you
but
compare the two code block ,it looks like String and Object are different in Garbage Collection?
Am i right,Steve??
3X
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

it looks like String and Object are different in Garbage Collection?

Not really. String objects on the heap may be GC'd just like any other object, when no references point to them. String literals are a bit of a different case, though, as a unique String object is created for each unique literal, and a reference to that object is stored in the runtime constant pool. There will be a reference to a String object pertaining to a String literal as long as the class is loaded.

In either case, though, a String object may be GC'd when that object is not accessible by any code. Just remember that String objects pertaining to String literals are accessible by a call to intern().

Hope this helps.
Pan Zhihua
Greenhorn

Joined: Aug 01, 2005
Posts: 9
Garbage Collection
What makes an object eligible for garbage collection? If you're preparing for the SCJP exam (or even if you're not), the answer to that question should roll right off your tongue. An object is eligible for garbage collection when it is no longer referenced from an active part of the application. Anyone see what is special about garbage collection for String literals? Let's look at an example and see if you can see where this is going.

Source Code

public class ImmutableStrings
{
public static void main(String[] args)
{
String one = "someString";
String two = new String("someString");

one = two = null;
}
}



Just before the main method ends, how many objects are available for garbage collection? 0? 1? 2?

The answer is 1. Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection. This is the same example as I used above so you can see what our picture looked liked originally there. Once we assign our variables, one and two, to null, we end up with a picture that looks like this:




As you can see, even though neither of our local variables, one or two, refer to our String object, there is still a reference to it from the String Literal Pool. Therefore, the object is not elgible for garbage collection. The object is always reachable through use of the intern() method, as referred to earlier.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

@Pan:

That's right; String objects may be GC'd just like any other objects. Objects that are accessible by code may not be GC'd. String objects containing the characters described by a String literal will always have a reference to them in the constant pool as long as the class is loaded.

Hope this is clear.
Pan Zhihua
Greenhorn

Joined: Aug 01, 2005
Posts: 9
To Steve:
That's OK *^_^*
Any way,thank you again!

heh,I'm in Amoy China,now's 1:00 AM
I think i should sleep
 
 
subject: doubt on garbage collection
 
Similar Threads
Garbage Collection????
garbage collection
GC question
garbage collection
garbage collection question