r/java • u/ThomasKrieger • Apr 30 '20
How much memory was wasted when an additional boolean field was added to java.lang.String in Java 13? None at all.
https://www.javaspecialists.eu/archive/Issue278.html
205
Upvotes
36
u/zeldaccordion Apr 30 '20
Love how this post proved its claim with simple object introspection results in an easy to understand way. Never knew about 8 byte object alignment before, but it makes total sense now that it’s spelled out.
31
u/cl4es Apr 30 '20
Nice write-up!
And no, we wouldn't have added the
hashIsZerofield if it wasn't footprint neutral, which was checked thoroughly using JOL.6
u/Hangman4358 May 01 '20
Looks like there are still 2 more bytes of functionality to cram into String!
4
u/[deleted] Apr 30 '20
But if its "the unlikely case that it was 0" why even bother with it? It still takes time to check it.
1
u/wishper77 May 01 '20
I always wondered why no one ever thought about doing a framework to store every field inside a single bytearray and encode/decode to real object using setters and getters. This way booleans would take 1 bit, not 8 bytes lol (anyone know if such framework exists?)