Calculate total memory usage of any object in Java

King J picture King J · Apr 9, 2013 · Viewed 7.1k times · Source

I'm using jProfiler. I have objects of a class X. This class has many properties of type String, Integer and other class types. I want to know how much memory is being consumed by a single object of class X.

When I use jProfile to find memory usage, I only get size of an object of type X. I'm assuming, jProfiler is not showing memory used by its member variables. For example, a String property of class X is contributing to the total memory used by String.

I want to figure out total memory usage of the class X including memory used by all its property objects. Any information on how to achieve that using jProfile or some other tool/code?

Thanks in advance!

Answer

Evgeniy Dorofeev picture Evgeniy Dorofeev · Apr 9, 2013

Take a look at this http://www.javamex.com/tutorials/memory/object_memory_usage.shtml. It contains info about how to calculate Java objects memory usage as well as references to the tools to calculate it.