wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Garbage Collection (again) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collection (again)" Watch "Garbage Collection (again)" New topic
Author

Garbage Collection (again)

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hey people, I know I did do a search but everytime I clicked back I lost the page, so it was taking me ages to find an answewr.
Anyway back to the point.

my questions are these:
in method1() the variable s1, is it eligable for GC when the method returns? same in method2() with the variable n.
Is variable s eligable for GC after the last print statement at end of programor does this not matter?
Arguments if in method2() would be GC'd after method returns?
It doesen't matter when you set a reference to null, if I reference it later on for say a print statement, it will not be GC'd
have i got the Garbage Colection correctly?
Davy


How simple does it have to be???
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
In Java, there are references and then there are objects.
In your code, s1 and s are references and are not the subject of garbage collection.
The objects that s1 and s refer to are the subject of garbage collection.
Given that, when s1 is set to null, the object that s1 referred to prior to being set to null, having no other reference, is eligible for garbage collection after the completion of that line. Not at the end of the method.
Likewise, when s is set to null, the object it referred to is eligible for garbage collection.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Thanks,
so in the

so the object of abc is eligable for GC at the line s1 += x;?
Davy
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
good point, yes, the abc object would eligible for GC after that line.
I think the fact that you're dealing with Strings creates some semantic issues that someone with a better understanding of the String pool can explain, but you've got the basics.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Davy,
It's me again! It's EXTRA TRICKY to use String objects when learning about GC because of something called the 'String constant pool' which makes String objects behave differently than every other kind of object. Plus, (and Marlene can correct me if I'm wrong), the handling of the String constant pool can be implemented differently by different JVMs. So use another kind of object when forming these questions... StringBuffer objects are good!
Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
thanks guys,
but my main concern was:

so its the objects not the variables that get GC'd?
Davy
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Hi,
Yes you are right.
If no references are referring the object,then it is eligible for GC.
Regards,
M.S.Raman
Originally posted by Davy Kelly:
thanks guys,
but my main concern was:

so its the objects not the variables that get GC'd?
Davy
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
thanks everyone,
just thought of something though, my tutor said, he said GC occurs when the method returns.
so:

sorry guys, but I was getting different answers from my distance learning tutor, by the way I am doing the SCJP 310-025 exam for the java 1.2 version
Davy
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Dudes, you will never guess, i got this GC question wrong!!! so far for understanding it.
I did the MindQ's Sun Certified Java Programmer Practice Test and I got a good score, some i knew i did not get, but the rest I was well happy with. 92% only 5 Q's wrong, but this garbage colection one was the one I just could not see.

Q36
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
Answers
a 0
b 1
c 2
d 3
e 4
I said 2

Here's how I looked at it:
name = frieda
newName = nick
newName = jason (1: nick is gone....dumped)
newestName = frieda
name = null (name does not have a partner, name found out frieda was two timing them with newestName, so name dumped her. name is now a hermit)
Ok I now see what I did wrong, I thought that because name = null that frieda went to see the bin men, but she is now having a relationship with newestName so therefore she is still around plying her wares on the heap.
So the answer should have been one.
Why is it that when I explain my probs to you guys, I sometimes answer it myself?
Davy
:roll:
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Why is it that when I explain my probs to you guys, I sometimes answer it myself?
http://www.c2.com/cgi/wiki?CardboardAnalyst


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
So Dirk,
is this a good thing???
Davy
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Yes. You're figuring things out.
Dan flex
Greenhorn

Joined: Apr 07, 2004
Posts: 3
Hey Davy, I do that all the time.I mean I'm about to ask a question on a forum, then I work it out myself.or after I've posted, I work it out
Of course that's totally ok, it's called learning
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Going back to your code method1,

is illegal. You can't assign a StringBufferReference to a String reference.

is better
Also, there are two objects involved, a StringBuffer object and a String object. The StringBuffer object is eligible for GC after

The String object is eligible for GC when method1 returns, since that deletes the stack variable s which refers to the String object.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection (again)
 
Similar Threads
About Overridden II (a Mock Question question...)
Garbage colln Q - help!!
Local inner classes
Object Creation & destruction
exam lab gc question