aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection question 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 question" Watch "garbage collection question" New topic
Author

garbage collection question

prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162


My guess is all 3 objects as after local method,all references are set to null??

Any guesses

Thanks
Prashanth


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Prashanth,
you wrote:
My guess is all 3 objects as after local method,all references are set to null??
That's what I though also, but it is not what the creators of these tests want to hear. At that very point, the method is not yet over (after a millisecond, it will be). But they're wanting us to really see where the references point to. And count the objects that are dereferenced at that point.
I guess, that example is from you?

Here is my solution, I hope it is correct:




I'm not ready with the counting yet, maybe tomorrow.

Yours,
Bu.


all events occur in real time
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Cool..
Thanks for your answer...

Prashanth
Fred Roger
Greenhorn

Joined: Nov 01, 2006
Posts: 19
whats the answer guys ???

I m confused I am getting 1 object eligible for GC at line 12. Please help
Gaurang Pathare
Greenhorn

Joined: Oct 31, 2006
Posts: 8
Originally posted by Burkhard Hassel:
Hi Prashanth,
you wrote:
My guess is all 3 objects as after local method,all references are set to null??
That's what I though also, but it is not what the creators of these tests want to hear. At that very point, the method is not yet over (after a millisecond, it will be). But they're wanting us to really see where the references point to. And count the objects that are dereferenced at that point.
I guess, that example is from you?

Here is my solution, I hope it is correct:




I'm not ready with the counting yet, maybe tomorrow.

Yours,
Bu.

IMO since the compiler is still in the main method only 2 objects will be eligible for gc, the once with the reference = null, i.e r2 and r4

may be this will help

[CODE]
class Rubbish {
public static void main(String [] args) {
Rubbish r1 = new Rubbish();
Rubbish r2 = new Rubbish();
Rubbish r3 = new Rubbish();
Rubbish r4 = r2;
Rubbish r5 = r4;
r2 = null;
r4 = r2;
r1 = r5;
// do stuff

System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
System.out.println(r4);
System.out.println(r5);
}
}
[/CODE}

OUTPUT ->

Rubbish@7d772e
null
Rubbish@11b86e7
null
Rubbish@7d772e

--------

r2 = null
and since r4 = r2, even r4 = null,
but r5 will still refer to the rubbish object from line 7, and hence r1 will aslo refer to same object.


Intellect distinguishes between the possible and the impossible; Reason distinguishes between the sensible and the senseless. Even the possible can be senseless.......
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Gaurang:

IMO since the compiler is still in the main method only 2 objects will be eligible for gc, the once with the reference = null, i.e r2 and r4

may be this will help

r2 = null
and since r4 = r2, even r4 = null,
but r5 will still refer to the rubbish object from line 7, and hence r1 will aslo refer to same object.


I too agreed with Gaurang.


The Best way to predict your future is to create it - Every great individual common man
David Grindley
Greenhorn

Joined: Oct 30, 2006
Posts: 14
Originally posted by Gaurang:
IMO since the compiler is still in the main method only 2 objects will be eligible for gc, the once with the reference = null, i.e r2 and r4


Only one object is elegible for Garbage Collection. In the following description the object names O1, O2, O3 are just a way of identifying the objects created on the heap

On line 3 an object O1 is created and is referenced by R1.
On line 4 an object O2 is created and is referenced by R2.
On line 5 an object O3 is created and is referenced by R3.

After executing line 6,
O1 is ref'd by R1;
O2 is ref'd by R2 and R4;
O3 is ref'd by R3;

After executing line 7,
O1 is ref'd by R1;
O2 is ref'd by R2, R4 and R5;
O3 is ref'd by R3;

After executing line 8,
O1 is ref'd by R1;
O2 is ref'd by R4 and R5;
O3 is ref'd by R3;

After executing line 9,
O1 is ref'd by R1;
O2 is ref'd by R5;
O3 is ref'd by R3;

After executing line 10,
O1 is ref'd by nobody and hence is elegible for Garbage Collection;
O2 is ref'd by R1 and R5;
O3 is ref'd by R3;

[ November 02, 2006: Message edited by: David Grindley ]
[ November 02, 2006: Message edited by: David Grindley ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi Guys,

The answer to this option would be...

Only one object is eligible for gc.

It dosn't mean that once the main method is over, the jvm shuts down. There may be other threads running apart from the main thread and those other threads may still be using the Rubbisn objects referred to by r5, r2, r3. The one referred by r1 will be eligible for gc.

I think the basic idea of the question is to check what happens when the main method finishes rather than exactly knowing how many objects will be garbage collected.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
At line 12 only 2 objects r2 & r4 are eligible for garbage collection.
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Official answer is 1.

Cheers
Prashanth
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Originally posted by Saurabh Vyas:
At line 12 only 2 objects r2 & r4 are eligible for garbage collection.


There is no object created for r4...Its only a reference...so answer is r2
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Sorry cowboys for confusing you. Was not my intention, I just made a beginner's error.

I wrote:
(hope is correct...)
ehem...


This is wrong!


My fault was, I pointed with this little arrows to the variables, which is wrong. I should have pointed to the object the variable was pointing to at that time point.


So I made two pictures, the first one shows where the variables point to after line seven, when all five variables are initialized. One, two, three refer to the objects, as they are made by new.





The situation at line eleven //do stuff is shown here:



So only the first object made is eligible for garbage collection.


Yours,
Bu.
---
following code only develops the one coded by Gaurang a little in that it shows the dying of object #1.



Output may differ on different systems (only the last line):
r1 ->Rubbish #2
r2 ->null
r3 ->Rubbish #3
r4 ->null
r5 ->Rubbish #2
aaaaaaaaarghhh! #1
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
Thanks Prashanth, I understood my mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection question