Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exam question regarding garbage collection

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I've encountered with this question in a certain exam:
Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. return oa[0];
16. }
When is the Float object, created in line 11, eligible for garbage collection?
A. Just after line 13.
B. Just after line 14.
C. Never in this method.
D. Just after line 15 (that is, as the method returns).

The correct answer is: B.
And my question is why? oa[0] still holds reference to this Object and it returns it to the method caller, so if there is still live regerence to this Object how can it be eligable for garbage collection?
 
I would have thought the answer was C myself.

Tim
 
There is an argument here that it is option 'D', because :

Float is an immutable object - so that when the method returns, a copy is in effect taken.

If it were a mutable type though, then it certainly would be 'C'.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
But isn't it merely the reference which is returned? A reference to the same Immutable object?

Tim
 
Well, the argument is that because a Float object is just a wrapper for a primitive, then no ... but to be honest I don't know ... another developer here mentioned it could be the case, because the reference is copied on return.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Mmmm. My instinct says that a wrapped primitive is still an Object. When the reference to it is returned, the reference is copied with the copy still pointing to the initially created immutable wrapper object. But that's as deep as my understanding goes.

Tim
 
Maybe the JVM has a cache for Floats similar to the Strings one. If so, the reference would be garbaged while the object itslef would be cache for future use.

Cheers,
Dian
 
I understand this. I'm just not sure if I can explain it... so here goes nothing:
Code:
10. public Object m() { 
11. Object o = new Float(3.14F);  //a primitive value of 3.14 is created and referenced by o
12. Object [] oa = new Object[1];
13. oa[0] = o;                    //The value of o (3.14) is "copied" to oa[0] in memory
14. o = null;                     //the reference to o is deleted, but the value of o is still accessible by way of oa[]
15. return oa[0];                 //the oa[0] returns the value of 3.14 
16. }
after line 14, there is no longer a reference to 3.14 by way of o and therefore the memory of o is no longer needed and therefore eligible for garbage collection.

Like I said, I'm not sure this is clear to anyone but me.

"If you say you can, or you say you can't, you're right!"
-- Henry Ford
 
acent said:
The value of o (3.14) is "copied" to oa[0] in memory

I disagree. I think it's the reference to the Float object which is copied, not the value. It makes all the difference. Java doesn't go in for copying objects all over the place when doing simple assignments, surely.

Consider this code:-
Code:
    Float f1 = new Float(3.14);
    Float f2 = f1;
    Float f3 = new Float(3.14);
    System.out.println("Object equality f1 == f2 " + (f1 == f2));
    System.out.println("Object equality f1 == f3 " + (f1 == f3));

This yields
Code:
Object equality f1 == f2 true
Object equality f1 == f3 false

If the first assignment copied the object rather than the reference, the first equality check would have yielded false since the two copies would be different objects which just happened to contain the same wrapped value of 3.14

Tim
 
@accent:
11. Object o = new Float(3.14F); //a primitive value of 3.14 is created and referenced by o
a) Not a primitive value, but a Float-Object is created, and referenced by o.
b) The question has been:
When is the Float object, created in line 11, eligible for garbage collection?
Nobody was interested in 'o'.

seeking a job as java-programmer in Berlin:
 
stefanwagner, you are right, it is an object and not a primitive. My mestake in posting.

timw, again, I have to admit, you're right about copying.... that was a bad choice of words on my part.

As for if the question asking when the object is eligible for garbage collection, and the answer of B, I would like to ask thelordoftherings to ensure the question asks about the object rather than the variable of o. Given a mistype in the post, I stick to my guns on B. If the question truely asked about the object, I would now change my answer to agree with stefanwagner.

"If you say you can, or you say you can't, you're right!"
-- Henry Ford
 
Then the answer is correct. The object o is eligible for garbage collection after it's set to null.

Cheers,
Dian
 
Nope. The 'Float Object' created at line 11 is the Float object created by the 'new' keyword. The Object variable o has been nulled at 14, but the Float Object it referenced is still referenced by the Array Object which is returned from the method and is not garbage collected.

Tim
 
Remember that objects are separate from the object variables which reference them. They are only eligible for garbage collection when no object variable references them. If the Float in the above question was garbage collected at 14, then the reference returned from the method would suddenly end up pointing at nothing at some undetermined time down the line.

Tim
 
That's because the object o itself can be garbaged and the Float not.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top