aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  Garbage collection-Marcus exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Garbage collection-Marcus exam" Watch " Garbage collection-Marcus exam" New topic
Author

Garbage collection-Marcus exam

Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here

If somebody could give me a good explanation about the answer to this question,it would be great.Do the rules vary in any way when it concerns wrapper classes?I mean x,y are references to Integer objects right?Or do they just store the integer values?
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi,
According to me the answer should be 0.
public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
}
public BettyAck() {
Integer x = new Integer(10); //obj1 created
findOut(x); //sets temp variable to null, original obj1 intact
Integer y = new Integer(99); //obj2 created
Integer z = y; //refrence pointing to obj2
z = null; //ref set to null
findOut(y); //sets temp variable to null, original obj2 intact
//here
no objects eligible for GC

}
public void findOut(Integer y){
y = null;

}
}
But after the constructor returns, 2 objects will be eligible for GC.
Hope this helps.
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

Do the rules vary in any way when it concerns wrapper classes?I mean x,y are references to Integer objects right?Or do they just store the integer values?

x, y are references to Integer objects.
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Thanks kathy.I had assumed the answer to be zero.But now I got the explanation too.
Anish Doshi
Greenhorn

Joined: Jun 13, 2003
Posts: 23
Hey,

I think three objects should be eligible for garbage collection. Can anyone correct me if I am wrong.
Kind Regards,
Anish Doshi.


Sun Certified Programmer for Java platform 1.4
Rahul JG
Ranch Hand

Joined: Jun 05, 2002
Posts: 44
Cathy is right!!!


Rahul
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
Originally posted by Rahul JG:
Cathy is right!!!

hello,
I too agree that cathy is right. c Anish could u pls justify ur reply.


Mona(Varijasmom)
Anish Doshi
Greenhorn

Joined: Jun 13, 2003
Posts: 23
Hey,
Xtremely srry its 2 objects. my mistake.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I try to create helpful answers and explanation to all of my questions. It would be useful if you can quote that answer/explanation, and highlight anything you do not understand.
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Hi Cathy,
Mind to help me understand this better?
Initially I thought it would be 3 objects.
1. temp (y) in findout()
2. z
3. temp (y) in findout()
The variable y in findout() was created 2 times (different object), am I right? Or is it because the second findout execution actually 'reuse' temp (y)?
Sorry to trouble you with silly question.
Thanks.
CH
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Hi everyone,
Sorry to ask such silly question. Would appreciate if some one could guide me thru my previous question. I am very new to Java concept, so really need everyone's help.
Thanks!!!
CH
Anish Doshi
Greenhorn

Joined: Jun 13, 2003
Posts: 23
Hey Lee,
The thing is that there are only two objects, the 1st one is the one that was refered by variable x (which is made null) and the other refered by variable y. So when
Integer z = y;
is executed both variable y and z refer to the same object. Then both are made null one by one, so three reference variables are null(ed). But actually they altogether refered to only two objects. And now these two objects are elligible for garbage collection.
Hope this helps.
Kind Regards,
Anish Doshi.
prasanthi alla
Greenhorn

Joined: Dec 02, 2003
Posts: 15
hi nair!

the answer is zero,
explanation is simple, basic garbage collection principle
1.an object is eligible to be garbage collection only if all the references variables pointing to them r set to null.
2. when we pass an object as a parameter to a method 'a copy of its reference is send'
so in ur code two objects r created , which r still having reference variables x and y pointing to them
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
To simplify things around here,

when sending a reference of an object as a parameter to a method, only a COPY of that reference is sent.

That is why the answer is 0.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
But what about Integer Y in the program?
It is explicitely made null and the findout() method is not called using it.
Isn't it not eligible for garbage collection?
Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
well 0 objects will be eligible for garbage collection bcoz we r still in the constructor but once the constructor is finished 2 objects will be eligible for garbage collection bcoz only 2 objs were created and three refrences were created
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
posted by mrudul:

But what about Integer Y in the program?
It is explicitely made null and the findout() method is not called using it.
Isn't it not eligible for garbage collection?

The 'y' in the BettyAck() and in the findOut() are not the same.
Let me demonstrate what really happens:
y (in the BettyAck()) --> new Integer(99)
After calling findOut()
y (in the BettyAck()) --> new Integer(99) <-- y (in the findOut())
Now setting y (in the findOut()) to null
y (in the BettyAck()) --> new Integer(99)
y (in the findOut()) --> Null
As you clearly see the object new Integer(99) is still being referenced by y (in the BettyAck())
Hope this clarifies you.
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Hi Vicken,
Thanks for your clarification and I would like to ride on your illustration hope you don't mind.
Actually, I am quite puzzled with the y in FindOut(). When FindOut() was executed the first time, it create a variable holding y.
Then, when FindOut() was called the second time, would it create a new y or 'reuse' the y that was created previously?
Hope you can help me out here.
Thanks.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
posted by CH Lee

Then, when FindOut() was called the second time, would it create a new y or 'reuse' the y that was created previously?

'y' in the findOut() is a local variable, it exists as long as the findOut() is executing when you exit this method then all its variables are gone. Each time you call findOut() a new 'y' is created (This 'y' has nothing to do 'y' in the BettyAck().
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Hi Vicken,
Thanks for the explanation. Yet, I still need to trouble you. Sorry about that.
Since y in FindOut() is created everytime FindOut() is called, then shouldn't it be 3 objects available for garbage collection?
Initially I thought it would be 3 objects.
1. y in findout() -- First execution
2. z
3. y in findout() -- Second execution
Am I right to say so?
CH
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

...shouldn't it be 3 objects available for garbage collection?

Not at all . You are confused between objects and references. 'y' is a reference to an object, it is NOT an object. A copy of the reference is passed as an argument to findOut().
Take this for example:
1) 'x','y' are references for object 'A' (remember 'A' is an object while 'x' and 'y' are only references)
x --> A <-- y
2) Now, let us set the reference y to NULL (y = Null)
x--> A <-/- y --> Null
3) What happened here is that 'y' lost its reference to the object 'A'
4) 'A' of coarse is not EGC because it is still being referenced by 'x'
Note: objects are always created by the new clause.

I still need to trouble you. Sorry about that.

There is no trouble... we are all here to help. If you still have any other question i'll be more than happy to answer it (if i can of coarse)
Anish Doshi
Greenhorn

Joined: Jun 13, 2003
Posts: 23
Hey folks,
I think the above class wont pass the compiler, as the method 'findout()' isn't a static method. And a constructor can have access only to the static members of the class.
Kind Regards,
Anish Doshi.
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
Hi Vicken,
This is getting more and more intersting. I am enjoying this forum now!!!
First, let me try to understand the problem presented in this thread.
As I understand, a variable passed to any function is by value. Am I right to say that this simply means that a new object will be created to hold the value? My understanding is that it can't be a copy of the reference passed in, as this would simply changed the value of the original object. Is this the reason why I saw some mock exam questions where a value is not changed eventhough a called function actually did some changes to it?
If my understand is correct, does that mean a new object (y in our case) would be created whenever FindOut() is executed?
I've to admit that I am very new to Java, so I am still very vague in term of how and when object and reference are created. Thanks for pointing out to me that object is creating with the new keyword. I think I will re-read the byte story presented here in JavaRanch to refresh my mind on object and reference.
Vicken, would appreciate if you could knock the confusion out of my mind.
Cheers
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I go to great effort to create these questions and to create matching answers to try to explain the points in them. I am sure that I do not always succeed, but it may be of great if you have problems with the question to quote the answer. Debating these questions AND answer can show up quirks, thus as a result of this query I notice I repeat the word pointer (which may not even be an appropriate analogy anyway)......The answer I have given to this question is as follows...
0
A reference passed into a method is passed as if it were a copy of a pointer pointer rather than the actual object. Thus if that reference is assigned to a null it makes no difference to any other copy of that pointer. Thus the code within the method findOut makes no difference to any other references. Although reference z is assigned to null reference y still points to the object so no objects are eligible for garbage collection.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Hi CH Lee,


And yet some more:
1) Objects are never passed as an arguments to methods. I repeat, only a copy of the objects reference is sent.
2) Sending a copy of the reference to a method will NOT change the value of the object.

Now after you have done reading 'byte story' and still have further questions then you just go ahead and drop them here, the ranchers here will knock your questions out... and that is Guaranteed.
Note: I recommend, you do exactly what Marcus suggested. The answers he provided for his questions are simple and clear, however if you for any reason don't understand them then simply quote that part and we will try to clear things up.
[ December 04, 2003: Message edited by: Vicken Karaoghlanian ]
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Hey CH,
This code is from SL-275, it will not compile however, because MyDate class is not available. You can trace it, and see the output posted at the buttom.

This code outputs the following:

Int value is: 11
MyDate: 22-7-1964
MyDate: 4-7-1964

The MyDate object is not changed by the changeObjectRef method; however, the changeObjectAttr method changes the day attribute of the MyDate object.
CH Lee
Ranch Hand

Joined: Nov 24, 2003
Posts: 42
I got it!!!
Thanks Marcus and Vicken for going great length in explaining this to me. I understand where did I go wrong in this question.
Integer is a wrapper class, which means this is not Java primitive value. So calling FindOut() is pass by reference, not by value as I argued previously. The Java primitive is int. Sorry for posting silly questions when I am not even sure about what Java primitive value is.
z is not GC because it is a reference!!! Not an object.
Thanks guys. I think I better start reading my Java revision books again.
Also thanks to the author of the article Pass-By Value here in JavaRanch. It's great and easy to understand.
Cheers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage collection-Marcus exam