• 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

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I am doing Diagnostic Exam from Devaka's Exams. I am feeling difficulty in Garbage Collection. Can anyone explain me how to proceed to get how many objects are eligible for garbage collection after Line 12....

Answer is : 1

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you initialize a2 at Line 8 you are passing a new object A to the constructor and then, the instance variable "a" of a2 refers to that new object.
In Line 12, that instance variable is set to null, so there is no reference to that isolated object.

The rest of the objects still have a reference to them.

(I think I'm having problems explaining myself :lol: )
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me draw an ASCII map of what is happening




When a1.a=new A(null); is executed, this happens



and after execution of a2.a=null; //Line12 , this happens

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

Ankit Garg wrote:
and after execution of a2.a=null; //Line12 , this happens


Its a2.a=null;
So this should happen


Banu Chowdary wrote:
I am feeling difficulty in Garbage Collection. Can anyone explain me how to proceed to get how many objects are eligible for garbage collection


The way Ankit explained it..........that's exactly the way to go about solving GC questions.
Always draw the objects on paper as they are created and make a cross when their reference is removed in the code.
In such a way, in the end you can see how many objects are left isolated.
If Ankit can do it in the message, you should be able to do it on paper.......
Practice a lot(on paper). You should do fine after some experience.

Ankit,
You really explain well, such types of questions......on the forum....
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sachin for correcting my mistake
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is the Object in line 1 eligible for garbage collection???

the finalize method is running and printing output "-" . I already posted a question about garbage collection. But still in confusion....


Is the Object in line 1 available for Garbage Collection???
 
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
IN both the code the object created is eligible for GC. because there was only one reference and that is also set to null.
The only difference is that in first code you are requesting GC to do its work.
I the second one. Its left to GCr. It would do if it feels like doing GC...
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James.. Now Cleared..
 
Honk if you love justice! And honk twice for tiny ads!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic