Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Garbage collection problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 8998
19
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!