Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

no of instances and Object's size

 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any method to determine number of object instances of a particular class currently in memory and to find an object's size.
thanx in advance
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple answer: No.
Not so simple answer: There might be something in the debug API, but I am not sure - and it certainly isn't meant for daily use.
What do you need this for?
 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u Ilja Preuss ,
i asked this just to know if we can determine the amount of memory required for our application to run
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could get a rough measure by using a combination of Runtime.getTotalMemory() and Runtime.getFreeMemory().
Beware, though: The result might heavily depend on the JVM used. Not only will it depend on when the GC decides to collect unused objects. Java also doesn't specify how data is implemented. For example, a char variable only has to behave as if it were a 16 bit value - JVM implementors are free to use any amount of memory (on some processor architectures it is faster to acces memory on "word boundaries", so a JVM for a 64 bit word processor might be implement to always use multiples of 64 bit).
 
John O'Hanley
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the measurement of object size, here
is an example which may be of assistance :
http://www.javapractices.com/Topic83.cjp
It follows the scheme of the "Java Platform Performance" book.
It is designed for classes which have a no-argument constructor. (If your case does not
allow this, you will need to modify, perhaps using the Constructor.newInstance method).
[ October 19, 2002: Message edited by: John O'Hanley ]
[ October 19, 2002: Message edited by: John O'Hanley ]
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a particular class or two you are interested in, and you can modify or extend that class, you might be able to do your own instance counting, using a static field you increment in the constructor and decrement in finalize.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic