aspose file tools*
The moose likes Mock Exam Errata and the fly likes Objects eligible for GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Objects eligible for GC" Watch "Objects eligible for GC" New topic
Author

Objects eligible for GC

H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
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

Can someone take a look at this. Thanks
Reghu Ram Thanumalayan
Ranch Hand

Joined: Oct 21, 2003
Posts: 193
The answer should be zero as String literals are not eligible for garbage collection.


Cheers,<br />Reghu Ram T<br /> <br />SCJP 1.4 - 98 %, SCBCD 1.3 - 94 %, SCMAD 1.0 - 92 %
Bajji Pat
Ranch Hand

Joined: Apr 05, 2005
Posts: 50
Pls take time going thru this..
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
After going through the article, i would go for 0 Strings. since none are created using the 'new' operator, none are created at runtime and hence none are eligible for GC
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hi to all,

how could be this possible
for example
if we write the stmt like this
String x="xyz";


then the compiler will automatically creates an object internally.
otherwise we could not able to use its methods

for ex we can srite this stmt

x.concat("abc");

so what i want to say is, there will be an object for a string literal
so that we can use its properties.

am i right?if not
can anyone plz justify this issue


cinux


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Kevin Alterman
Greenhorn

Joined: May 21, 2005
Posts: 12
The answer should be choice b, 1.

First off since "name" is not explicitly initialized to null I will assume these are class level variables that are implicitly initialized ;-)

Now, a new object is created when newName is assigned "Nick" but it is immediately dereferenced since newName is assigned to another string object "Jason". The "Nick" object is eligible for GC since it is in a dereferenced state. name is assigned to new String object with value "Frieda" but the reference is transferred to newestName so it will not be dereferenced when name is assigned null.. so "Frieda" is not eligible for GC since it can still be used using the newestName reference ;-)

Kevin

Originally posted by HS Singh:
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

Can someone take a look at this. Thanks
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
From everything I've read they don't get garbage collected, but the do a heck of a job optimizing the "pool".

I did my best to try and "fill up the pool" from simple string concatenation in an infinite loop to random character generation and assembly into Strings. I then tried to watch a performance monitor to see if the memory usage climbed. The CPU would spike but after a very "small" initial memory consumption (...may not even be related to the string pool")it leveled off and remained steady even though it was still "looping". So in the backgroud it is either cleaning up after itself or doing some form of optimization with substrings.
[ May 26, 2005: Message edited by: Byron Estes ]

Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for GC