Do Java Records actually save memory?

Discussion in 'Programming' started by _ProfliX_, Jun 14, 2021.


Do Java Records actually save memory?

  1. Yes

  2. No

  3. Yes and no

  1. :) Do Java Records actually save memory over a similar class declaration or are they more like syntactic sugar?
    Do they or is the memory using the same? :unsure:
  2. This StackOverflow answer provides a pretty good overview. Its mainly the same besides the toString, hashCode, and equals implementations. Overall, I'd argue that its diminishing returns at this point and its honestly fine to use either way works for you (although records are a really nice time saver imo).
    • Informative Informative x 1
  3. The advantage should be in the way the memory of records can be managed from Garbage Collector? So, could there be any "noice" server performance improvements with Aikar flags or just the same thing no matter class or record.
  4. That stackoverflow answer focuses on class file size which is for sure completely irrelevant. Its about runtime memory usage.

    Afaik records are not smaller in runtime on the jvm. It also shouldn't change the speed to the garbage collector.

    There is no class metadata which mutable classes have, but records don't.

    For small records the hotspot part of the jvm might at some later point introduce call-by-value internally for small records (normally java is call-by-reference for all non-primitives) to improve speed.
    • Useful Useful x 1
  5. Aikar should also do some extensive testing with the new ZGC garbage collector. Its claimed to be faster, have shorter gc breaks and do more stuff async.
  6. Aikar has talked about ZGC before in spigot & paper, said it's not a good replacement for g1. Has some issues with minecraft's allocation rate, and needing a fuck load of memory to be decent. Recommend searching for "zgc" from aikar in either discord yourself for a better idea
  7. Maybe this? :unsure:
  8. Maybe that you confused records with the future primitive classes ( that will effectively reduce memory usage by removing the class identity