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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top