4 ms·
Where is the bloat? There are default behaviours for equals and hashCode, so you never need to define them if they're not useful for your Object.
by admax88q 5y ago
Where is the bloat? There are default behaviours for equals and hashCode, so you never need to define them if they're not useful for your Object.
- titzer 5y agoDid you read the article? The JVM reserves ~4 bytes in the object header of every object to store the identity hash code, regardless if you override .hashCode() or not.
- t-writescode 5y agoCompared to all the other bloat in Java, this is pennies
- titzer 5y agoIf you have an object that has only one field, it still consumes at least 3 words of memory; 2 for the header, and 1 for the field. That's a 200% memory overhead. Of course that's extreme, but I've seen estimates as high as 40% of memory space the heap in large Java applications is just object headers. You probably need at least 1 header word in any scheme, but 2 is wasteful IMHO.
- e_y_ 5y agoThere's a way to save memory by sacrificing .hashCode() runtime performance (on the assumption that most objects never get hashed) by keeping a separate lookup table for hashcodes, at the cost of having to do the indirect lookup. I don't recall which VM used that technique. There's also fun tricks like rewriting/optimizing the object header during a copying/compacting garbage collection.
- titzer 5y agoYeah, I know that trick. I implemented an identity hashmap[1] inside of V8 that uses that, because JS objects don't have an identity hash code[2]. It uses the object address as the basis of the hash code, which means the table needs to be reorganized when the GC moves objects, which is done with a post-GC hook. The identity maps in V8 are generally short-lived. I am not aware of production JVMs that use this, because in the worst case they can use a lot more memory if a lot of objects end up needing hashcodes. [1] https://github.com/v8/v8/blob/dc712da548c7fb433caed56af9a021d964952728/src/utils/identity-map.h https://github.com/v8/v8/blob/dc712da548c7fb433caed56af9a021... [2] I guess they might have an invisible one now; I haven't looked at the implementation of WeakHashMap/Set.
- admax88q 5y agoWhat objects do you have lots of that you're not storing in a HashMap or similar?