aspose file tools*
The moose likes Java in General and the fly likes no of instances and Object's size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "no of instances and Object Watch "no of instances and Object New topic
Author

no of instances and Object's size

sheril she
Greenhorn

Joined: Oct 08, 2002
Posts: 12
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
sheril she
Greenhorn

Joined: Oct 08, 2002
Posts: 12
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Oct 13, 2002
Posts: 24
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

Joined: Feb 22, 2001
Posts: 399
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: no of instances and Object's size