0

There could be a situation when an object of size 16 bytes occupies two cache lines, i.e., it starts on one cache line and ends in the next cache line. Fetching such an object requires two cache line reads, which could be avoided would the object be aligned properly. In C++11 we can use the alignas keyword, but I’m wondering how java manages to do it, if at all?

wayne
  • 598
  • 3
  • 15
  • 1
    That probably depends on the JVM implementation but you'd normally not need to bother. If such optimizations are really relevant, Java might be the wrong language for your purpose. – Thomas Apr 05 '23 at 07:46
  • Perhaps there is something useful [here](https://stackoverflow.com/q/44468639/3788176) (not a direct dupe). – Andy Turner Apr 05 '23 at 07:49
  • The alignment and field layout is JVM dependent (the layout changed in openjdk15). A good resource is shipilev's [Objects Inside Out](https://shipilev.net/jvm/objects-inside-out) and his [Java Object Layout](https://github.com/openjdk/jol) library. The exact details cannot be influenced by the developer, but one can optimize algorithms such as using blocked bloom filters to be cache line friendly (e.g. Caffeine's [FrequencySketch](https://github.com/ben-manes/caffeine/blob/00f58622af2c68d86102b7488a5cfa02c65e5461/caffeine/src/main/java/com/github/benmanes/caffeine/cache/FrequencySketch.java)). – Ben Manes Apr 05 '23 at 18:50

0 Answers0