• 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 doubt

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

In the below code how many objects are eligible for garbage collection when the method m2() begins to execute.

I thought answer is zero. If not, please explain me why?

class I {
private String name;
public I(String s) {name = s;}
private I other;
public void other(I i) {other = i;}
}
class J {
private I i1 = new I("A"), i2 = new I("B"), i3 = new I("C");
private void m1() {
i1.other(i2); i2.other(i1); i3.other(i3);
i1 = i3; i2 = i3;
m2();
}
private void m2() {/* Do amazing things. */}
public static void main (String[] args) {
new J().m1();
}}

Regards,
Surekha.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi surekha,

Two objects are eligible for garbage collection when the method m2() begins to execute.

Because i1 and i2 are now reference variables pointing to the Object which is pointing by reference variable i3.

The reference to the Objects created before are now free. They are not referenced by any reference variables.

Correct me if I am wrong..

Regards
krishna.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Two objects are eligible for GC.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Draw object <--> reference diagram. Then it will be easy to derive the answer.
 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for the response.

Still i have a doubt.

Here we are passing the references i1 and i2 to other() method and we are storing these references. So how can they eligible for GC?

I think i'm missing something. Please explain..

Regards,
Surekha.
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi surekha,

I sent one jpg file. Did u receve that one.

I hope u understood.

Regards,
Krishna.
 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hari,

I didn't receive any file.
Please explain me the logic. It will be much helpful.

Regards,
Surekha.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii hari,

can I know what is object <--> reference diagram,im new to this group
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
Since Member Variable(reference) other in i1 and i2 refer each other now, objects referred by i1 and i2 are not eligible for garbage collection. is nt it?

Any explanation would be helpful..
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No... GC can't just keep any objects just because there is a cyclic reference b/w them. Here eventhough i1 and i2 are having internal reference to each of them, these objects are not still reachable from any part of code. Hence they will be GC-ed
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

you are right laxmanan arunachalam.

see the image.

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