• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to know the objects created for the class  RSS feed

 
ravindar mynampati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

what is the code to get how many objects were created for particular class right now in the heap memory of JVM.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no simple way to find out how many objects have been instantiated of a given class. You could write a class that keeps track of how many times the constructor is called:

If you want to do this for any class, without the need to include special code like that in the class, then you can probably do this using the debugging API of the JVM. Have a look at this if you really want to dive into that: JavaTM Platform Debugger Architecture (JPDA)
 
ravindar mynampati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all thanks for reply,
i think the code which you have posted counts the number of objects created so far for that class, not the exact number of objects present on the heap of that class.
 
Karan Rajan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, maybe you could use finalize() to reduce the count. So everytime an object gets garbage-collected the count is reduced by one. Do you think that might work ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not really. the problem is that finalize is garanteed to be called once before the object is garbage collected, but not IMMEDIATLY before. In other words, finalize might be called, but the object not GC'd for 5 minutes (or more...). So the object could still be on the heap, but you've decremented the counter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!