Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on garbage collection

 
Pui Shing Lee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One String object will be eligable for garbage collection, "Nick".
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One String object will be eligible for collection i.e. "nick"
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx Steve!
 
Kanchan K Bulbule
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your example reference to Nick is lost

Please read the following: String literals
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic