• 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

Instance count

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to find the number of instances running in a JVM of a particular class
or
if Class A is running and in another class i want to provide the name and get it instance running in JVM

Thanks in advance
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hardly an advanced question.

I don't understand the second part. Do you mean Class.forName().newInstance()?

The first part isn't easy; you would have to set a private static count field in your class, and put "count++;" in your constructor. A getCount() method will tell you how many instances have been created. As for how many have been lost, much harder. The nearest I can think of, would be to put "count--;" in the finalize() method. Of course finalize() is only called when the object is garbage-collected, so the count field might be wildly inaccurate.

But I don't think it is real object-oriented programming for a class to keep a record of how many instances it has; it is for client classes to decide how many references they hold, and even then there might be multiple references to the same object.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Campbell says, you can add code to a specific class to count the instances. I agree with him that wanting to do this hints at poor design, although one cannot be sure without knowing your reason for wanting to do it.

If you are looking for a way to find the number of instances of any arbitrary class, there is no way to do this with the ordinary Java API.

The JVM has several debugging, monitoring and profiling interfaces these days. I believe that one or more of these will be able to tell you how many instances there are of a particular class. But these interfaces are not intended to be used as part of an application; it might not even be possible to do so.
 
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
Perhaps you can use java.util.WeakHashMap.
This class will not keep a "real" reference to its keys, so if an object's last reference is the map then it is elligible for garbage collection anyway.

You can use null for the values, and just use keySet() and containsKey().
 
Chandraprabha Rajput
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot..
what i actully need is :
I have a class A and and it is running .

Now i am creating an another Class B in which i will be passsing the class name(i.e A) and it should diplay the number of instance of A running in JVM.
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want to do that?

Also, you need to sort out your terminology. In Java, one does not "run" a class. When one creates an instance of a class, the correct term is "instantiate". In Java, the things that you can "run" are: (1) a Thread, (2) a Runnable (using a Thread), (3) a program.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Campbell said , you know how to get static count variable from Class A ,

so make a method which takes Class as a parameter and call its static "getCount(() method !!
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic