File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Mohan Chandrasekaran
Greenhorn

Joined: Nov 05, 2005
Posts: 18
This is related to making a reference to null for String object

Say we have the following code:

1.String s1 = "SCJP1.4"
2.String s2 = "SCWCD1.4"
3.s1 = null;
4.s1 = "SCJP1.4";

Here when we make the reference s1 to null, whether the String object referenced by the literal namely "SCJP1.4" is eligible for garbage collection.

I read like literal itself a reference to String object and that when you try to assign the literal to String reference,the reference will be pointing to String object. So when s1 is made null. The literal referring to String object will be exisiting.


Thanks,<br />Mohan<br />SCJP 1.4(93%)
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
Thumb rule in java: whenever any object is unreachable it is eligible for garbage collection.

1.String s1 = "SCJP1.4"
2.String s2 = "SCWCD1.4"
3.s1 = null;
4.s1 = "SCJP1.4";

In this case, s2 is eligible for garbage collection after line 4 is executed. this is because String object (in the string pool) with value "SCWCD1.4" is unreachable.

But s1 not. since it is reused at line 4. object "SCJP1.4" at line 4 is basically same as line 1. since string literals are always taken from the string pool.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228

In this case, s2 is eligible for garbage collection after line 4 is executed. this is because String object (in the string pool) with value "SCWCD1.4" is unreachable


This is what I feel , Objects in the String Pool will not be GC'd.
also in the above case s2 is not at all set to null. So none of the objects will be eligibe for GC
[ December 19, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection