Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to measure memory usage?

843798Aug 1 2001 — edited Aug 2 2001
Hi all!

Is there any more sophisticated way of getting the memory requirements of objects at run-time?

Currently I'm using Runtime.totalMemory()-Runtime.freeMemory() to estimate the memory usage before and after loading the object in question. The difference should be the memory used by the object. I also call upon Runtime.gc() to make sure that the garbage collector is run before each of the measurements. Unfortunately Java doesn't guarantee that the garbage collector is really run and that all unused objects are removed from memory even if it is run.

Although this method gives you a rough indication of memory usage (at least for very big objets), I would like to have something more precise (and repeatable). Using the method above on two big objects created in different order leads to different results.

mneumi
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 30 2001
Added on Aug 1 2001
2 comments
208 views