aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC (enthuware question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC (enthuware question)" Watch "GC (enthuware question)" New topic
Author

GC (enthuware question)

Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
Hi All,
This is the question from enthuware:

After which line will the object passed to the method getObject() be eligible for garbage collection?



Answers: //1, //2, //3, //4, None of these
And the correct answer is: None of these
Enthuware Explenation:
"All the assignment operations are just adding the confusion. The point is that the objects passed to a method can never be garbage collected in that method. The reason is simple - such objects are created outside the method and so there are references pointing to these objects outside the method. The references inside the methods are a mere copy of the outside references. Making these null, will not affect the outside references."

I answered //3, because I thought that the invocation of method getObject may look like this:

getObject(new Object())

so, am I wrong ?


SCJP 6.0 (87%)
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Before reading the enthuware explanation I answered 3. The reasoning for "None of the above" makes sense to me but i dont see this explained anywhere even when I googled. When an Object reference is returned from a method to the calling method then the Object is not eligible for Garbge collection. but when an object ref is passed as an argument, no where it says it cannot be garbage collected.
Can anyone throw some light on this??


If you worry you cannot work... If you work you need not worry
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Przemek Malirz:

After which line will the object passed to the method getObject() be eligible for garbage collection?



Answers: //1, //2, //3, //4, None of these



My answer before looking at the correct answer was none of thesee.
I don't know how far i am right but the way i saw it was ,
that all obejects ie. all references are pointing to only one object -b.
That means a,b,c are all pointing to one object.

b = a = null; //4
After line 4 , a and b are null , however, c is still pointing to the same object and therfore still not eligible for GC.

c = null;
After this line, all the three objects are null and there for this object is elegible for GC.
This line is however not mentioned in the answer, so i thought the answer would be None of these.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Hmmm...tricky question....Looks like the enthuware mock exam is good. Can anyone tell me whether whizlabs is better or enthuware???


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
IMHO:
This answer (None of these) is good only when invocation of getObject() method looks like:

Object obj = new Object();
getObject(obj);
// 1

then when we ahieve line // 1 there still will be reference "obj" to an Object.

But, when we invoke this method in other way:

getObject(new Object());

then the only reference to newly created object is a local (method) variable/parameter "a". In this case correct answer should be // 3.

What do You think about it ?
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 106
public void getObject(Object a){
Object c, b = new Object(); //1
//do something with a
c = b; //2
a = c;//3
b = a = null; //4
c = null;}

In this we are not constructing the object a .we are only getting the refrence of the object from ooutside the function .it is not the local object for this function . in this code there is no possibility that garbage collector will call the object a in this function.


Cleared SCJP 1.5 | Cleared SCWCD 5.0
(SCEA/OCMJEA) in progress.....
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
Again.

The question is: "After which line will the object passed to the method getObject() be eligible for garbage collection?"

So, why do you assume that we are geting the existing refrence of the object from outside the function. Why do you think that there exist any reference to this Object outside the method ?

To the method is passing an Object !, so this function might be called like this:



And this is a big difference.
Enthuware question doesn't tell that outside getObject method exist reference variable to this Object, and that this variable is passed to this method.

Enthuware explenation is incorrect:
"...The point is that the objects passed to a method can never be garbage collected in that method. The reason is simple - such objects are created outside the method and so there are references pointing to these objects outside the method..."
Wrong !!! There might be no reference at all !!!
- we have object created outside the method and no referencing variables pointing on them besides local method variable (parametr "a") !!!

?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Przemek Malirz in any case the object will not be eligible for garbage collection in the method getObject. if you pass it (new Object()) then the object will be eligible for garbage collection when the call returns and the statement of the call executes completely..

so

getObject(new Object());
//now the object is eligible for garbage collection....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

And this is a big difference.
Enthuware question doesn't tell that outside getObject method exist reference variable to this Object, and that this variable is passed to this method.


During the test are you allowed to question the question or must you answer it? During the test, when you are facing an unknown, and hence, can't be sure of two possible answers, should you assume the common case? Or should you assume the fringe case? Or should you just not make an assumption and not answer the question?

Enthuware explenation is incorrect:
"...The point is that the objects passed to a method can never be garbage collected in that method. The reason is simple - such objects are created outside the method and so there are references pointing to these objects outside the method..."
Wrong !!! There might be no reference at all !!!


Actually, you don't know if Enthuware is incorrect here. The registers may be saved as part of the method call, and since the last thing the registers were doing was building the stackframe, the object may still be accessable -- until the method call is done, and the registers are cleaned up.

Regardless, back to the first point -- isn't it better to assume the more common case. And answer accordingly? Isn't the goal to pass the test?


Having said that, this is an interesting discussion, and I would be interested to see where it goes....

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
Thanks for your answer.

So why below code produce an output:
1
getObject.A
Huge.finalize
getObject.B
2


Puneet Nahata
Greenhorn

Joined: Aug 30, 2008
Posts: 9
public void getObject(Object a)
{
Object c, b = new Object(); //1
//do something with a
c = b; //2
a = c;//3
b = a = null; //4
c = null;
while(true){}
}

and the call to this method is: getObject(new Object())

Since this method call would never return, that means Object a will never be eligible for garbage collection. Am i right???
Or is it eligible for garbage collection at line 3???

Please clarify my doubt.


do not anger the GODS.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3200
    
    2
Originally posted by Przemek Malirz:
Thanks for your answer.

So why below code produce an output:
1
getObject.A
Huge.finalize
getObject.B
2



Hello,
The output proves nothing. GC thread is different from current thread. Their outputs can be interspersed. When I ran the program, I got -


Try this code -


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Puneet Nahata
Greenhorn

Joined: Aug 30, 2008
Posts: 9
So the conclusion is that if we pass an Object reference to a method then that object will never be eligible for garbage collection inside that mehtod.

But if we pass an actual object, then that object can be eligible for garbage collection inside that method.

Am i right??
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
IMHO: Yes !
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
And this is what I try to prove.

"if we pass an Object reference to a method" an enthuware question is correct, and "if we pass an actual object" the question is incorrect.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3200
    
    2
Originally posted by Przemek Malirz:
And this is what I try to prove.

"if we pass an Object reference to a method" an enthuware question is correct, and "if we pass an actual object" the question is incorrect.


I think you may be right. Let us see the following java code and the generated bytecode -


If you analyze bytecode for m2(), you will observe -
0: load 'this' reference on stack
1: create a new object of class Object and put the reference on stack (let's call it 'new')
4: duplicate the reference on top of stack (which is actually the reference to new object i.e. duplicate 'new')
5: call constructor on the new object (the dup reference is thereby removed and the 'new' is now on top of the stack)
8: create a new stack for calling m1, call m1 on 'this' and pass 'new' ('this' and 'new' are now popped, reference to the stack frame of m1 remains on the current stack)
11: pop the reference of the stack frame of call to m1.
12: return

Number 8: is important. It seems that the references this and new are popped from the current stack frame and there is no reference to the new object on the current stack frame anymore.


Contrast the above with the java code and byte code for another similar program -

Here, at 7: the reference to new is stored in the local variable table for the method m2.

I am not 100% sure if I am reading the bytecode right. Still investigating...
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

The explanation is truly justified. You never know how many references are there to the object that is passed to the method. In the method it looks that we are removing only one reference, but there can be other references in the block that called this method.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
I don't think so. The explanation try to tell us that the object passed to the method getObject can never be garbage collected, and i think that's not true!

"...The point is that the objects passed to a method can never be garbage collected in that method. The reason is simple - such objects are created outside the method and so there are references pointing to these objects outside the method..."
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Hmmm...Interesting....
Przemek Malirz
Greenhorn

Joined: Sep 02, 2008
Posts: 11
I still think that this sample output

show us that an object inside the method was garbage collected between calls
getObject.A and getObject.B.
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177



Hi ,
In your call to the Garbage Collector, you are just making a request to the JVM to garbage collect, when the JVM does it you dont know(it may do as soon as it executes the System.gc() or it may delay it till the end of the program, so in my opinion this code cannot predict the output from this program. I also think that there are some assumptions to be made for this question to be answered. And the safest assumption would be to assume that the object passed MAY have references. You dont know how this method is called so you have to make this assumption. I fully agree with Henry Wong in this case. There could be more scenarios attatched to this problem, so the safest assumption to make is that the object MAY have references outside the method so its NOT GUARANTEED that the object would be garbage collected inside the method.


Preparing for SCJP 6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC (enthuware question)
 
Similar Threads
object references in a method GCed??
Unfair Question on JQ
Garbage collection?
Garbage Collection on method arguments (from Enthuware)
GC ??