• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Garbage Collector  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, first of all I want to apologize for my bad english and I'm not even sure if this is the proprely thread to submit my question.
That's my question:
In this code, which one of the 3 objects i1,i2,i3 IS NOT elegible for the Garbage Collection(And why) when the method g() starts:
Java Code:


Options are
A= o1
B= o2
C= o1 and o2
D= o3
E= None of previous answers

I thank you in advance,
Greats, Yuri.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you tell us what you think the answer is and the reasoning you followed to arrive at that answer and then we can discuss your thoughts.
You'll learn a lot more that way than just being given the answer.
 
Yuri Attanasio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.
This question was on my Java exams, i took A, o1 but I think it may be wrong. I just focussed my attention to this code lines: "i3=i1, i2=1", I haven't studied GC very well, but I seem to remember that if we got two objects , x and y, and we do : x =y, than x loses his referments and he's elegible for the Garbage Collection. So in this case i3=1, i2=1, it seems like i1 is the only object not elegible for GC, and that's why I answered o1. But then I realized, it should have been i1 in the answer(instead of o1?) and still, does the method referTo influences my procediment? Well i guess they do, i don't know why but i think my answer is more likely wrong.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's get the terminology right first
i1, i2 and i3 are reference variables. They refer to objects, but are not actually objects
So, at the end of line 13 you have created three objects (o1, o2 and 03) and they are referenced by i1, i2 and i3 respectively.

Now, assume for the moment that lines 15 - 17 don't exist.
On line 15, you assign i1 to i3. That means i1 and i3 now both refer to object o1 and there are no variables referring to object o3.
On line 16, you assign i2 to i3. That means i1, i2 and i3 now all refer to object o1 and there are no variables referring to objects o2 and o3.

However, lines 15 - 18 do exist and they call the referTo method which sets the ref variable in each object to point to a given object.

So now you need to work out which objects the ref variable in each of your objects points to after lines 15 - 18.
Once you've done that you will know which variables point to o1, which to o2 and which to o3. If any of those objects don't have a variable pointing at them, they are eligible for garbage collection.

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Which exam was it?
 
Yuri Attanasio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I assume that after lines 15-18, every object is pointed by an object and then the correct answer was E because they are all NOT elegible for the garbage collection, am i right or am I loosing something?
 
Yuri Attanasio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was my "Programming languages" exam at the IT university of Naples, Italy. (I'm not sure i spelt it correctly )
 
Yuri Attanasio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more precise, o1 is pointed by i2.ref, o2 is pointed by i1.ref and o3 is pointed by i3.ref
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now here's where it gets complicated

i1, i2 and i3 all refer to o1.
And the ref variable in o1 refers to o2.
So o1 and o2 are referenceable and not eligible for garbage collection.

So that leaves o3. Now there is a reference to o3, but that is from the ref variable in o3.
However because that reference to o3 is actually inside o3 itself, the garbage collector is clever enough to realise there is no external reference to o3 and so it knows it is eligible for garbage collection.

So I would say the answer is D C (whoops - the question was which are not eligible), but I had to think about it for a while so I'm happy for someone to contradict me or (better still) confirm my thoughts.
 
Yuri Attanasio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean C because the question was which one were " NOT ELEGIBLE", correct? It really makes sense what you said so i think that's right
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, what a poorly written question. Thank you for confirming its source.
I have reformatted the code because it wasn't correclty indented as you had it. That sometimes happens in exam questions; they indent it wrongly in the hope of confusing you.
I think you are right about C; i1 and i2 still have reachable references.
 
Yuri Attanasio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for the answers and for your time. I'm just asking one last thing, i read somewhere that static variables are never elegible for GC even if they have no references, is that true?
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuri Attanasio wrote:I'm just asking one last thing, i read somewhere that static variables are never elegible for GC even if they have no references, is that true?


It's neither true nor false. That's because variables are never garbage-collected. Garbage collection works on objects, not on variables. So what you read might be talking about objects which are referred to by static variables.

If that's the case, then it's like this. An object referred to by a variable (static or local or whatever) cannot be garbage-collected until that variable goes "out of scope". A local variable goes out of scope when the code being executed leaves the block which contains that variable. An instance variable goes "out of scope" when the object containing that variable has no references. Now, if that instance variable is a static variable then the object containing it is the class where it's declared -- and it's very hard for a class to be garbage-collected. It's not impossible, but to know all about it you have to know about class loaders, which isn't a beginner subject.

In other words it's actually more complicated than what you read wherever. But if you're trying to answer those questions in the certification exams, the ones with the toy programs with horrible code, those tricky technical issues aren't going to arise and you can assume that objects referred to by static variables can't be garbage collected.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuri Attanasio wrote:You mean C because the question was which one were " NOT ELEGIBLE", correct?

Yes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!