• 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

How many objects eligible for GC??

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

class Car {}
class Ferrari extends Car {}
class Camry extends Car {}

public class GC
{
public static void main(String[] args)
{
Car a1 = new Ferrari();
Car a2 = new Camry();
Car a3 = a1;
a1 = null;
Car a4 = a1;
Car a5 = a2;
a1 = a2;
a2 = a4;
a5 = null;
a3 = a1;

// Here
}
}


My doubt is while GC wether we must count reference varaibles equal to null or count objects without any references .Please someone help me in understanding this.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess objects not pointed by any reference variables are eligible for gc.we garbage collect objects not reference variables.
for example if an object is pointed by 2 variables and one of them is set to null,it is still being pointed by the other and hence will not be collected.

gc can also be called when there exists an island of isolation.but that is not the case with your code.by the time the execution reaches //Here
all reference variables are null,hence both objects will be eligible for collection
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my calculations only ferrari object is eligible for GC.
a3 and a1 are still pointing to camary at //here.

Am I wrong?
[ December 04, 2007: Message edited by: Varalakshmi Ramanarayan ]
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a matter of fact my calculations were wrong.
Only ferrari is eligible.

my sincerest apology
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a matter of fact my calculations were wrong.
Only ferrari is eligible.

my sincerest apology
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks for all your reply...
Only ferrari is eligible for Gc...means 1 object...
We should not consider reference varaible a2=a4=a5=null into count.Because if count them then its 4 objects available for GC.

Please someone tell me whether whatever i have understood is correct.
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that a1 and a3 are still pointing to Camry and Ferrari is eligible FWIW, I have to draw pictures for the GC stuff. I use solid lines to objects and then when a reference 'breaks away' I use dotted lines. Works for me.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an example of how to use a diagram-based approach to GC questions, check out Burkhard's ultra nifty post from an older thread.
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question..
If objct A is = object B
and Object B = Object C,
Then would it automatically mean..
Object A = Object c ??
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic