• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

garbage collection eligibility

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



how many objects are eligible for garbage collection and why?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shashank

Why don't you tell us what you think the answer is?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't my program.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After line 19, i think 2 objects are eligible.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Md. Minhajur Rahman wrote:After line 19, i think 2 objects are eligible.


Nobody told me if my thought process is right?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic