Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection (again)

 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
so in the

so the object of abc is eligable for GC at the line s1 += x;?
Davy
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys,
but my main concern was:

so its the objects not the variables that get GC'd?
Davy
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it that when I explain my probs to you guys, I sometimes answer it myself?
http://www.c2.com/cgi/wiki?CardboardAnalyst
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Dirk,
is this a good thing???
Davy
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You're figuring things out.
 
Dan flex
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic