-
Couldn't load subscription status.
- Fork 126
Description
I worked on a JVM memory measurement library that used reflection to analyze deep object graphs and found a very significant performance improvement by simply caching shallow sizes. I don't have references but I believe it was on the order of 10 times faster for some common scenarios.
The implementation would be quite trivial by creating a new CachedMemoryMeterStrategy class which contains:
- a HashMap cache from class to shallow size
- a regular MemoryMeterStrategy to be used as a fallback when measuring a new type of object that hasn't been encountered before
Scenarios where this is extremely beneficial:
- Collections and arrays usually store objects of the same type (or a small number of subtypes)
- Collections themselves can wrap every item ending up with many wrappers of the same type (such as Node in HashMap)
- Tree structures usually contain a very small number of types (eg. subtree & leaf nodes)
Even if an object graph contains no repeated classes, this allows us to re-use the same MemoryMeter instance to measure a large number of similar objects so that the cache from the first measurement speeds up the measurements of subsequent objects.