| Author |
garbage collection eligibility
|
shashank dwivedi
Ranch Hand
Joined: Mar 06, 2012
Posts: 57
|
|
how many objects are eligible for garbage collection and why?
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
shashank
Why don't you tell us what you think the answer is?
|
 |
Md. Minhajur Rahman
Ranch Hand
Joined: Apr 10, 2012
Posts: 33
|
|
Hello shashank , Those Objects are eligible for garbage collection which are not accessible by any active reference. According to your program, at which line you want to count Objects eligible for garbage collection? You have created two object inside main funtion() and inside main() all are accessible. If once you come to outside the main function(), then this two will be eligible for garbage collection.
|
 |
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 657
|
|
|
This isn't my program.
|
 |
Mittal Vishal
Greenhorn
Joined: Nov 13, 2006
Posts: 21
|
|
Shashank,
I guess you are asking for the objects eligible for garbage collection after the line 19 is executed.
And the answer is 1 because only those Objects are eligible for garbage collection which are not accessible by any active reference.
As per your program, you have created 2 objects at line 17 & 18 but after assigning b2 to b1, original b1 object does not have any active reference so it will be eligible for garbage collection
Thanks
Vishal
|
 |
shashank dwivedi
Ranch Hand
Joined: Mar 06, 2012
Posts: 57
|
|
James Boswell wrote:shashank
Why don't you tell us what you think the answer is?
well i think it should be 2.The reason why i think this is because i have created objects of child class
which should call constructor of its parent class.
One object dies here should mean two objects eligible for garbage collection.
I am not sure though if i am right..
please help!
|
 |
shashank dwivedi
Ranch Hand
Joined: Mar 06, 2012
Posts: 57
|
|
shashank dwivedi wrote:
James Boswell wrote:shashank
Why don't you tell us what you think the answer is?
well i think it should be 2.The reason why i think this is because i have created objects of child class
which should call constructor of its parent class.
One object dies here should mean two objects eligible for garbage collection.
I am not sure though if i am right..
please help!
And ofcourse i am talking after line 19.
|
 |
Md. Minhajur Rahman
Ranch Hand
Joined: Apr 10, 2012
Posts: 33
|
|
|
After line 19, i think 2 objects are eligible.
|
 |
Pondurai Singh
Ranch Hand
Joined: Jun 27, 2011
Posts: 32
|
|
In above code, two objects of type B are created at lines B b1=new B(); and B b2=new B(); . But b1=b2; b2 references the same object as b1 and both are thus again aliases. The object originally referenced by b2 now becomes eligible for garbage collection.
So, in both cases, b1 and b2 point to the same object and any changes done to that object will be seen through both the references. Keep in mind that b1 and b2 are not the object themselves but only references to that object. b1=b2 doesn't copy any values from one object to the other, it simply repoints the reference.
|
Software Developer, Oracle Java Certification Training Labs at EPractize Labs
OCPJP 7 | OCAJP 7
|
 |
shashank dwivedi
Ranch Hand
Joined: Mar 06, 2012
Posts: 57
|
|
Md. Minhajur Rahman wrote:After line 19, i think 2 objects are eligible.
Nobody told me if my thought process is right?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
shashank dwivedi wrote:
well i think it should be 2.The reason why i think this is because i have created objects of child class
which should call constructor of its parent class.
One object dies here should mean two objects eligible for garbage collection.
I am not sure though if i am right..
please help!
Just because two constructors are called to initialize the object, doesn't mean that two objects were instantiated. BTW, using the same argument, since class A subclasses from the Object class, aren't three constructors call?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
shashank dwivedi wrote:
shashank dwivedi wrote:
James Boswell wrote:shashank
Why don't you tell us what you think the answer is?
well i think it should be 2.The reason why i think this is because i have created objects of child class
which should call constructor of its parent class.
One object dies here should mean two objects eligible for garbage collection.
I am not sure though if i am right..
please help!
And ofcourse i am talking after line 19.
IMO, I guess it depends on what you mean by "after line 19". If you mean, exactly after execution, and the "b1" local variable is still in scope, then one object is eligible for GC. If you mean, the next line of code, where the main() method completes, and the "b1" and "b2" variable are no longer in scope, then two objects are eligible for GC.
Henry
|
 |
 |
|
|
subject: garbage collection eligibility
|
|
|