• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following,

After line 6 runs. How many objects are eligible for garbage collection?
0
1
2
3
4

Answer is 1.
can anyone explain what will happen to the object, referece variable mx is pointing to?
[ September 13, 2005: Message edited by: Michael Ernest ]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line is supposed to be line 6?
 
putti don
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have marked the line 6 with a comment. (//6)
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


X x=new X(); //This object has a reference at the end of line 6

X x2=m1(x); //An object is returned from the method and x2 holds the
//reference

X x4=new X(); //This object has a reference at the end of line 6

x2=x4;//6 //x2 is relinquishing its reference to the original object
// and is pointing to the object stored in x4
//Remember that you are creating objects using new operator...

So at the end of line 6 you have one object (orginally created by the method execution ) that doesnt have any reference & ...so can be "GC"ed...

Regards
 
putti don
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
are x2 and mx pointing to same object after the method m1() returns in line 4?
Thanks.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the variable mx will not be in scope after the method returns...sure enough within the method..it does point to the object..created..and after it returns....x2 points to the same object..

but within main....mx is not available..


Hope u got it...

 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mr. Manogna Alias Sanju,
I'm Not Agree With U That x2 is pointing 2 same object
Instead Now x2 Is Pointing To x4.Just Stick To Pointer Diagram.


Agrah
3rd Year B.tech
Bioinformatics.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi agrah,

What Manogna was referring to the line where the method call is happeneing...

that is before the statement x2=x4;

But after x2=x4...yes it does point to x4..

Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic