• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tracking objects created by my class

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

How do you keep track of how many objects are created by the existing class.?

I believe we can have a static int count which is incremented in the constructor. how do we decrement this count before the object is nullifed or destroyed. So that at any given time i am able to understand how many objects are there currently which are generated from my class?


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

You can override the protected finalize() method present in Object class. decrement the static count in this method.
It is called whenever any object is garbage collected by JVM.



Hope this works...
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nilesh,
I am interested in more details. Can you please tell us why do you want to track this? Is this just for the curiosity or your application demand this in/directly?
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I was thinking about finalize but not sure if it was the right way.

I was investigating this as i had read this question for interview on some site.

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

Nilesh Raje wrote:I was investigating this as i had read this question for interview on some site.


Then finalize method fits as a good option as suggested by Sunny.
 
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
edited
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two parts to this problem: Increment count when a new object is constructed, and decrement count before it is garbage collected. To do the second part I think the only possible way to do it is using finalize(), but even that can be potentially misleading, since just because an object is finalized it doesn't mean that it is garbage collected. What would happen if the JVM runs finalize() on an instance, the count is decremented, but then the object somehow becomes ineligible for GC? The JVM doesn't garbage collect an object immediately after calling finalize, it first checks again to make sure there are no references to the object. So there could be issues with count not being very accurate. A similar thing can happen on construction. What if you increment the count when an object is being constructed, but then there is a problem later during the construction and the object is not fully constructed? Then you will have an offset of +1 in the count, and since an object that is not constructed can't be finalized, this discrepancy wouldn't be eventually solved via the finalize() method.

I wonder if there is a more powerful mechanism (using reflection maybe) to solve this problem.
 
James Tharakan
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
EDITED
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edited.
 
James Tharakan
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
EDITED
reply
    Bookmark Topic Watch Topic
  • New Topic