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

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


i have seen these type of code and he is asking that after which line object created in line1 is eligible for garbage collection?

i am confused here

one object is created at line 1

after executing line 2 the instance var of that object is referencing itself...

LINE3: when a1=null then a1.a is null as a result object is eligible for GC or is it still referenced by a1.a?

my thought is that here a is an instance variable of object a1 so when a1=null then a1.a also null

am i right?

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no a1.a is not null, it is pointing to object created at line1, but you cannot reach a1.a now, it is now unreachable code.

In your code after executing line3, only one object is eligible for GC.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After line 2


After line 3


my thought is that here a is an instance variable of object a1 so when a1=null then a1.a also null



Not really,
when a1 is set to null, a is not set to null.
But since a is reachable only through a1, the GC decides that "since a1 is not reachable , no other object can access a1.a. Which implies that the object created at line 1 is not reachable. so it deletes the object."
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for posting a similar reply (like punit singh).
His post came few seconds befrore my post.
I think Punit has made the point clear
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi punit

are you sure?

here Ony one object is referenced by a1 as well as a1.a(instance variable of a1)..

if a1.a is not null then how can you say that one object is eligible for GC...?
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have seen that

when reference variable is null then instance varaible of that particular object which is pointing to the same object is also becomes null

in old posts...

can anyone give me final statement for my doubt?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static A a;

make this change.And answer yourself.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when reference variable is null then instance varaible of that particular object which is pointing to the same object is also becomes null



I have not read such statement anywhere, but I have read one concept that is
island of isolation. That is detected by GC.

See some discussion here in GC doubt.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

a1 will not be garbage collected because till a reference is pointing to and Onject instance in memory or pointing to null object i will not eligible for GC.
only when any new instance of a class is created and not reffered or pointed by any ref var , it will be GC.

example:
A a1 = null; // it will be eligible for GC but not gauranteed ti be GC immediately so it will Gc when exits the pgm.
B b1 = new B(); // will not be GC coz pointing to null object.
new B(); //will be GC coz it is not reffered by any ref var.
[ December 29, 2008: Message edited by: Ashika Chhabda ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused about this so can we take a step back!

The issue here is that after line 2 there are 2 objects on the heap: a1.a and a1.

If a1 is set to null, a1.a still continues to point to an object.

My understanding is that an object is eligible for garbage collection if it is "unreachable".
What does "unreachable" mean here? As a programmer I can't reach a1.a after line 3 because I'll get an NPE, so from my point of view a1.a is unreachable. So you could say both a1 and a1.a are unreachable and eligible for gc (2 objects are eligible after line 3).

However according to Khalid and Mungal, "an object in the heap is said to be reachable if it is denoted by any local reference in a runtime stack". So though we as programmers can't reach it, there's still a local reference to a1.a, so unless that is explicitly set to null, it is still reachable and not eligible for gc (1 object eligible after line 3).
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My understanding is that an object is eligible for garbage collection if it is "unreachable".
What does "unreachable" mean here? As a programmer I can't reach a1.a after line 3 because I'll get an NPE, so from my point of view a1.a is unreachable. So you could say both a1 and a1.a are unreachable and eligible for gc (2 objects are eligible after line 3).



No no you are going on wrong track.
First of all remember a1 and a1.a are not objects, they are just a reference to an object.
Here a1 and a1.a both referencing to same object that was created on line 1.

A a1=new A();=====================LINE1

So question is here, if you are creating just one object on the heap, how the GC will collect two objects from the heap?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic