Question

I have a Java project that initializes thousands of objects in a tree-like hierarchy.

Initially, I was programming with performance in mind and thus didn't mind storing a lot of computable properties in class fields(variables)..
However, recently, I started trying to estimate the memory footprint of my project and found it to be very large.

I'm trying to identify which class consumes the most memory
I guess I can then narrow it down to which field is consuming the most (probably Strings)

I have come across Java's instrumentation package and getObjectSize

I tried understanding their documentation, but I'm not sure if instrumentation is suited for my task.
I have also noticed that getObjectSize does not recursively find sizes.

I was thinking of creating a class that extends Object and contains a static map of instance -> instanceSize, and then making all my classes extend this class and also updating their constructors

Is my approach correct?
What would be the most 'Java' way of accomplishing this?

Was it helpful?

Solution

Um, put down the coding tools and pick up a memory profiler. there are great free ones out there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top