Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How manually check if object is available for garbage collection ?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we explicitly check if the object is eligible for garbage collection ?
 
Marshal
Posts: 26911
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming that "manually" and "explicitly" mean using Java programming? Well, think about it. An object is eligible for garbage collection if there aren't any active references to it. So... if you have a reference to an object then it isn't eligible for garbage collection. And if you don't have a reference to an object, then you can't access it through your Java code.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be grateful to you if you can explain the same with an example.
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you going to ask? if you say "is THIS object eligible for GC?", you surely must have a reference to it. Therefore, it is not eligible, by definition.

If you don't have a reference to it, how do you know it is there at all?
 
Paul Clapham
Marshal
Posts: 26911
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Divyya Joshi wrote:I would be grateful to you if you can explain the same with an example.


An example of what?
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In terms of answering OCPJP exam questions, I find the easiest way is to draw a diagram. List your variables down one side. In the other area, draw blobs for the objects. Draw arrows for references from variables to objects (and draw arrows from object to object when they hold references to each other). Step through the code, adding objects, and adding or crossing out arrows as appropriate.

Once you've gone through the code, you can then see which objects no longer have references pointing to them (or possibly you have islands of objects referring to each other). Those are the ones that are eligible for garbage collection.

I hope that makes sense - it's easier to draw than describe!
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mathew for this wonderfull idea

I tried your method to solve problem related to garbage collection but got stuck in that how can i align ( c3=c1.go(c2) in line number 10 ) in diagram .
Here is the code




 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be wrong, but I think that line 10 just makes c3 be null. Even though c2 is the argument to the method, nothing actually happens to c2, and the method just returns null, so c3 equals null.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic