• 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

What are the different ways to find number of alive instances of a class?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the different ways to find number of alive instances of a class?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a dump, and analyze it.

(No jokes intended)

WP
 
srikanth prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William P O'Sullivan for your reply and support.

Without peeping into the code (means not looking for new operator), Can I find how many alive instances are existed for a class?

In an interview of my friend, they asked this question.

What my friend replied is by declaring a static variable for the class... increment it in the constructor and decrement it in the finalize method.

Interviewer said this is ok but we can find it other ways also.

So I just want to know are there any ways to find alive instances for a class?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srikanth prasad wrote:Without peeping into the code (means not looking for new operator), Can I find how many alive instances are existed for a class?


No; at least not without extra code for every class you need the information for. That's the business of a Garbage Collector - and even then, only for those objects that can be collected when (or if) it decides to do it.

And your friend is wrong. Firstly, such a count will only work for a JVM session (and, as I said above, only for classes that include the code).
Secondly, the use of finalize() is not recommended for reasons that are too numerous to list here; the main two being that:
(a) it adds a disproportionate load to the JVM.
(b) you couldn't be sure at any given point how many of your objects were actually "alive", since finalize() is run by the gc, which you have no control over.

DON'T DO IT.

Winston
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the finalize() method is not a good solution. The method is not guaranteed to be called. The only thing you know about it is that it is called at most one time.

A slightly better solution is to use a static WeakHashMap, and let each instance store itself as the key; the value can be null. Because the WeakHashMap will remove its keys when they are no longer in use (unlike a regular Map or a List) it will not keep instances alive itself.

That's still not perfect though. First of all, instances will publish themselves before they are fully initialized. Secondly, this is not 100% accurate. Consider:
Try it like this, with line 19 commented out, with line 20 commented out, and with both commented out. You'll see what I mean.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srikanth prasad wrote:Interviewer said this is ok but we can find it other ways also.


There is one possible alternative, if you are only concerned with session counts: have your class implement the java.io.Closeable interface and implement your friend's suggestion through its close() method.

If you document it properly (ie, clients must close objects when they're finished with them) it might help but, at the end of the day, it'll only help you to track those who aren't using your object correctly; and you'll only ever be able to get counts of "unclosed" objects. The rest is down to good design.

Winston
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without mucking into the internals of any particular class, or altering the code in any way, try taking a look at the Java Virtual Machine Tool Interface. This probably offers multiple ways to count instances of a class; one is IterateOverInstancesOfClass. Or look at Troubleshooting Tools and try jhat or jmap to "take a dump", as William suggested.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srikanth prasad wrote:What are the different ways to find number of alive instances of a class?



That's a very open-ended question, and there's no one right answer. If that's what the interviewer asked, then there are two possibilities:

1. He's a lousy interviewer, or at least didn't put much thought into this particular question.

2. He wants to see about your thought process, give you the chance to explore options or ask for clarification, etc.

If it's #1, then he's probably looking for one specific (and arbitrary) answer. Even though many different answers might be technically correct for different interpretations of that question, he probably will only accept one particular one. Nobody here can know what answer he's expecting.

If it's #2, then there's still not one correct answer, but putting forth different possibilities, indicating their limitations, and asking for clarifications about the actual requirement, would be the way to go.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic