• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

garbage collection question

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

Any guesses

Thanks
Prashanth
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
prashanth kumar
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool..
Thanks for your answer...

Prashanth
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whats the answer guys ???

I m confused I am getting 1 object eligible for GC at line 12. Please help
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 12 only 2 objects r2 & r4 are eligible for garbage collection.
 
prashanth kumar
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Official answer is 1.

Cheers
Prashanth
 
prashanth kumar
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prashanth, I understood my mistake.
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic