• 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

Explain the output for garbage collection

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class GCDemo line1
{ 2
public static void main(String[] args) 3
{ 4
Object a,b,c; 5
Integer one =new Integer(1); 6
Float titanic =new Float(1.23); 7
StringBuffer str =new StringBuffer("sailing.."); 8
a=one; 9
b= titanic; 10
c=str; 11
a=b=c; 12
a=null; 13
titanic =null; 14
str=null; 15
System.gc(); 16


}
}

Identify the object that will be eligible for garbage collection after execution of line15

select any 1 option

1)object created at line 6
2)object created at line 7
3)object created at line 8
4)All of them
5)any of them.

I believe both objects at line 7 and line 8 is garbage collected.but the Question is to choose only one option.please let me know the closest one to choose.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think correct answer is 4. As line 15 is str=null, after execution of this line object refered by str will also eligible for garbage collection. So objects created on lines 6, 7 and 8 will be eligible for garbage collection.

Regards,
Anand.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say that only object created at line 7 is ready for garbage. Why?
Because variable one still holds its reference to Integer instance.
Variable c is still holding its reference to str.
The only instance that is not referenced is Float because non of a, b or c are referenced to it and variable titanic is set to null.
Or, b is set but later it is changed to reference on str.
[ November 24, 2006: Message edited by: marko salonen ]
 
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
Object created at line 7 is eligible for gc.

Reasoning:

At line 6, one = 1
At line 7, titanic = 1.23
At line 8, str = sailing...
At line 9, a, one = 1
At line 10, b, titanic = 1.23
At line 11, c, str = sailing...
At line 12, c's value is given to b which in turn is given to a, so in effect a, b, c, str = sailing... and titanic = 1.23
At line 13, b, c, str = sailing...
At line 14, No one is pointing to 1.23 after making titanic = null

So in conclusion, the object created at line 7 is the only one which is eligible for gc.
 
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 sailors,

I agree with Marko.

Given


after line 11 it looks like this:






after line 12:







after line 13:






and after line 14 & 15





So only the Float object is without any reference and eligible.

By the way: the topic is "Explain the output".
This is easier:
there is no output.



Yours,
Bu.
 
Joe San
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 Bu,

When I typed my answer, I thought of using circles and arrows....Can you please tell me which tool you use to draw those shapes for the objects??
 
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
I made it with Corel Draw and exported it to gifs (with transparency).

But there should be also easier ways to do it. Corel is no freeware.
Perhaps some other ranchers know some freeware links.

Yours,
Bu.
 
Joe San
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 Bu,

Thanks for the information.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic