Java Object [] and Caching

As you know, when memory is moved to L caches on the processor, it is moved using caches, thus optimizing cache performance ...

Well in java, when we define an jmm array, it guarantees that the memory for each element will be allocated sequentially. However, if we have an array of links, these links may randomly appear in different places in memory.

My question is that java allocates memory of real objects sequentially? What optimizations do we have under the hood for this?

For example, if we declare int [], we are sure that all of them are actually sequential in memory, but if we define NewType (for example, struct) in which there are two int fields and declare NewType [], java will compute and save is the actual memory sequential or not?

+4
source share
2 answers

My question is that java allocates memory of real objects sequentially?

This is not guaranteed, but most of the time the OpenJDK / Oracle JVM runs. In some cases this is not the case:

  • when you select a large object in a busy space,
  • your TLAB is full and you need to get another one.

However, in TLAB, it is simply allocated sequentially in memory.

declare NewType [] will java compute and store the actual memory sequentially or not?

Java , . , new .

+3

NewType (, struct), int-, NewType [], java ?

java , java , , , .

.. . " ".

.. :

HlRRRRRRRRRRRRRRRRRRRRRRRRR0HR0iii0HR0iii0HR0iii0HR0iii0HR0iii0HR0iii0HR0iii0
         Array             | Obj  | Obj  | Obj  | Obj  | Obj  | Obj  | Obj  |

H = object header
l = array length
R = reference
i = int
0 = various types of padding

jol .

JDK valhalla, , panama, .

, :

(, sun.misc.Unsafe), ByteBuffer/byte [] , API-.

+1

All Articles