Memory footprint estimation in Java

197 Views Asked by At

I am developing a class which will represent a set of rows from a table in Java. The rows could be in either compact or extended format. One can detect the internal format in the object using a function call.

I was trying to write a unit test to understand memory footprint of this object in compact and extended format. I used memorymeter https://github.com/jbellis/jamm) for this. I noticed that the deep size of the object is being shown to be different even when the underlying data remains the same in different runs. I am a bit confused with this. My expectation is that traversal through object graph should give me correct estimate of sizeof(object). Could this be an issue with the package I am using or is my expectation wrong?

1

There are 1 best solutions below

1
Edwin Buck On BEST ANSWER

If the code under test uses lazy initialization, or the test cases perform different operations, the actual instances in memory may differ in count, size, and occasionally even type.

I would verify that your different runs are using the same test case, and if they are not, I would not assume that the memory consumption with the same input would be equivalent.