wood burning stoves 2.0*
The moose likes Java in General and the fly likes What are the different ways to find number of alive instances of a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What are the different ways to find number of alive instances of a class?" Watch "What are the different ways to find number of alive instances of a class?" New topic
Author

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

srikanth prasad
Greenhorn

Joined: May 22, 2012
Posts: 4
What are the different ways to find number of alive instances of a class?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Take a dump, and analyze it.

(No jokes intended)

WP
srikanth prasad
Greenhorn

Joined: May 22, 2012
Posts: 4
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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2997
    
    9
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What are the different ways to find number of alive instances of a class?
 
Similar Threads
don't create too much object?
Different ways of loading a classes
employee system
Interview Question
no of instances and Object's size