2

I am looking for a way to programatically get the number of instances of a certain class type in .NET and Java.

Say for example I have class Foo. I want to be able to, in the same process, get a current count of all instance of Foo.
However I cannot modify Foo, so a static int with counting is out. Also I cannot just add all instances I make to some static list and count that. I want to be able to just say:

System.GC.numberOf< Foo >()

or something.

I was looking through the garbage collectors but I could not find any relevant methods.

Charles
  • 3,734
  • 3
  • 31
  • 49
  • 1
    there are duplicates to this. search around. – Bozho Apr 12 '11 at 19:07
  • there are similar questions but the answers always involve modifying the class with some sort of counter. I want to count instances of classes I do not define or control – Charles Apr 12 '11 at 19:10
  • well, you can't. that is also stated in some answers. – Bozho Apr 12 '11 at 19:14
  • The keep track of the number of objects you create in code you do control. – Security Hound Apr 12 '11 at 19:16
  • I have used tools to count instances of objects in remote processes, very useful for tracking down leaks. I am hoping there is a way to replicate this logic in the same program for testing purposes – Charles Apr 12 '11 at 19:23

5 Answers5

5

If you can't modify the class directly (perhaps because it is a built-in class?), could you create a wrapper, or a subclass that inherits the original?

public class subFoo extends foo
{
    protected static int count = 0;

    public subFoo() 
    {
        count++;
        super();
    }

    protected void finalize() throws Throwable
    {
        count--;
        super.finalize();
    } 

    public static int getInstanceCount()
    {
        return count;
    }
}

This example is Java and may have some syntax issues 'cause I'm a little rusty.

Of course, you'd have to be sure to redeclare all your foo as subFoo throughout the rest of your code.

Joshua Carmody
  • 13,410
  • 16
  • 64
  • 83
  • This did cross my mind, but unfortunately what I am dealing with are objects generated by a third party library. I want to count these objects while running automated tests to make sure they are being properly released. +1 for reading the question and offering a good solution though! – Charles Apr 12 '11 at 19:47
2

Do you have control of how the Java VM is being run? If so, you can write a quick and dirty debugger agent... http://download.oracle.com/javase/1.5.0/docs/guide/jvmti/jvmti.html#writingAgents

See the events "VM Object Allocation" and "Object Free"

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
2

Another somewhat exotic way to do it would be to use aspect-oriented techniques to instrument the constructor(s) of the class(es) in question. Take a look at AspectJ, for example.

mazaneicha
  • 8,794
  • 4
  • 33
  • 52
  • Going to shoot for this solution since I can probably instrument .net classes too in some other similar way. – Charles Apr 15 '11 at 14:43
1

As already commented, there are similar questions in SO.
One hack you can use - a big one IMO - is to change the Object class: see this answer

Resume:

  • copy the source of Object
  • add counting to its constructor (finalize)
  • add method to read the count
  • prepend the directory with the compiled class to the boot classpath (-Xbootclasspath)
Community
  • 1
  • 1
user85421
  • 28,957
  • 10
  • 64
  • 87
0

Quick and dirty: if you can't change the class, maybe you can just put a counter in another part of the project, incrementing it when you instantiate said class?

Lupuss
  • 649
  • 2
  • 11
  • 21