Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt on GC

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a doubt about the object being garbage collected below shown is the class

public class test
{
public static void main(String args[])
{
1//test obj1=new test();
2//test obj2=new test();
3//obj2=obj1;
4//obj1=null;
5//obj2=null;

}


}

can anyone explain me in which line the object obj1 is elligible for garbage collection? and why?i am a bit confused with line 4 or 5.

Thanks in advance.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is on the Line 3? Since there are two objects referring the same thing,object2 which is made to point the thing which object1is pointing.At this line it is now eligible for the GC.Am i right??

[ July 10, 2007: Message edited by: Ganesh Kumar ]
[ July 10, 2007: Message edited by: Ganesh Kumar ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ganesh
first i was confused with the answer being line 4 or 5 but now the answer is line 3.
i did not get the point if two objects are pointing to the same thing does it mean they are elligible for garbage collection?
how can this work please ellaborate and explain.
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually object1 was pointing to say 'dhwani' in the next step it('dhwani') was made to point by object2.Now there are two object pointing the same right.So object1 will be eligible for garbage collection.Now you have got it i think.!!

I too was not very confident in the garbage collection.I saw this in the Headfirst book
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well ganesh!!
i understood that object 1 is elligible for GC at line 3 ,but as per i know about GC it is said till all the references of object are not "null" it is not elligible for GC,in the line 3 we assign the refernce of object1 to object2 now object 2 holds the reference of object1 ....so how come object1 is elligible for GC?i think you are getting what i meant to say.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
can anyone explain me in which line the object obj1 is elligible for garbage collection?

After line 5. Let us look at this step by step.

Line 1: A new object is created (say Object1), and the reference variable obj1 points to it.

Line 2: A new object is created (say Object2), and the reference variable obj2 points to it.

Line 3: We assign a copy of the reference variable obj1 to the reference variable obj2. After this step, both variables points to Object1. Object2 is eligible for garbage collection.

Line 4: We set the reference variable obj1 to null. After this step, there is still the variable obj2 that points to Object1.

Line 5: We set the reference variable obj2 to null. After this step, there are no more references to Object1. Object1 is eligible for garbage collection.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!i agree with the explanation given above so the answer is obj1 is elligible for GC after line 5.Thank you for this explanation.
 
Space pants. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic