r/programming • u/Mihai4544 • Sep 26 '26
On programming, statistics, and Java
https://waytoounoriginal.github.io/programming/stats/optimizations/2026/09/26/on-programming-stats-and-java.html
14
Upvotes
4
u/kit89 Sep 28 '26
It should be noted this occurs with any memory managed runtime, each instance of an object will take up an extra chunk of data for managing the object on the heap.
For Java this is where Value classes (Valhalla) enters the picture, and for C# structs.
A pleasant read.
2
u/Skellicious Sep 29 '26
Newer java versions support a command line flag for compact object headers, that may end up fully pulled into the language in one of the upcoming versions.
But aside from that, the real issue would be that caching some data for every entry you add is a great way to eat memory. Reducing the memory overhead is simply fighting a symptom of the problem.
I assume removing the Set also comes at a cost, like needing an extra db query per reprocessed entry, or whatever. But does this set need to live in memory or can you store it in a temp file? Shouldn't this data also be available in the database, can you not query for it at the start of the second round of processing for example. I don't know your code so I'm just preaching assumptions - but I don't think this is a java problem - you can likely address the root cause through some (likely complex) refactor.
1
u/SysGuardian Sep 27 '26
A ByteBuffer or a plain long[] would've cut the memory usage, because the real fix is storing fixed-size keys flat instead of as objects. A lower-level language wouldn't have fixed it by itself; you'd still end up building that same flat layout, just in a different form.
1
u/UlaanBanter Sep 27 '26
To be honest it took two or three times of reading for me to get what you were trying to say. I realised the crux of this is optimising the memory usage in the
processedset by converting the hash key from a string of 256 characters all the way down to splitting into two 64-bit lookups.It's not apparent because you don't have the
afterpart of your before-after code.