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

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


How many objects are eligible for garbage collection??
The mock exam said that 4 objects were eligible..how come i4 is also eligible???
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Four objects would be eligible for gc only if i1, i2, i3 and i4 are assigned null value.
In the above example i1,i2,i3 are making a loop, but since they are not null, they are still reachable and thus not eligible for gc.
i4 has a self referencing loop. Therefore if i4 were assigned to null, it would be eligible for gc too.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai to all,

I think none of them are eligible for GC.
then How come it is 4?
can u please explain me?
thank u.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the real exam will list a line number associated with such questions - so for instance this question should say something like "after the line that invokes m1() completes"... otherwise the answer is often different depending on where in the code you look.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the answer is
None of the objects are available for gc after the method is invoked
4 objects are eligible for gc after main finishes execution..

Correct me if I am wrong
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after m1 completes, objects originally referenced by i1,i2,i3,i4 will be elegible for GC. (i1,i2,i3 and i4 are local variables of m1() and nothing outside objects referenced by i1,i2,i3,i4 references these objects)

(i4 - after m1() finished, the object is only referenced from itself and so it is unreachable.)

After line new J().m1() also the J object will be elegible for GC - its reference is not stored in any variable.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 4 objects are eligible for GC, since none of the object are referenced outside that method. If I am wrong please correct me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic