4

As we know when memory is moved to L caches on cpu it is moved with cachelines, thus the whole cache strading performance optimization...

Well in java when we define an array jmm guarantees that memory for each element will be allocated sequentially. However if we have array of references, those references can point randomly to different places in the memory.

My question is does java allocate actual objects memory sequentially? What optimizations do we have under the hood for this?

For example if we declare int[] we are confident those are all actually sequential in memory, but if we define a NewType (like struct) that has two int fields in it, and declare NewType[] will java figure out and keep actual memory sequentially or not?

3
  • well maybe i am wrong . memory allocation and JVM cannot be mixed . JVM does dynamic memory allocation as it feels (technically as the resource availability is best at that point of time) Commented Jun 27, 2015 at 15:38
  • 1
    A single object is allocated in a single chunk. Related to that is the fact if a GC is Depth or Breath first copying. Both variants exist in Hotspot. Here you find some aspects of it: oracle.com/technetwork/server-storage/ts-6434-159339.pdf Commented Jun 27, 2015 at 18:54
  • Didint quite understand "GC is Depth or Breath first copying" can you please give few links on this? (googled it but nothing reasonable found) Commented Jun 27, 2015 at 19:11

2 Answers 2

3

My question is does java allocate actual objects memory sequentially?

This is not guaranteed, but most of the time the OpenJDK/Oracle JVM does. Some of the times it doesn't are;

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

However, within the TLAB, it just allocates sequentially in memory.

declare NewType[] will java figure out and keep actual memory sequentially or not?

Java doesn't figure out anything, nor does it go out of it's way to allocate objects randomly in memory. In general, each new object will be immediately after the last one.

Sign up to request clarification or add additional context in comments.

5 Comments

just to mention - the GC may move allocated objjects, so even if they were allocated sequentially at first, there is no guarantee that they will stay sequential forever
@SvetlinZarev True, however, again the GC doesn't go out of it's way to arrange objects randomly in memory. Instead the order of discovery usually determines locality.
so this means no matter what we do, if we have array of reference types we loose cpu cache friendlyness?
I mean that we might have for instance a LinkedList that spans across two consecutive G1GC regions. And the GC cycle might collect garbage only from the first region, and then evacuate the remaining elements into another region somewhere in memory, so now not all elements will be consecutive.
@SvetlinZarev correct, however most of them will be so in terms of performance it will make little difference.
1

but if we define a NewType (like struct) that has two int fields in it, and declare NewType[] will java figure out and keep actual memory sequentially or not?

In this scenario java is not very cache-friendly because apart from primitive types java arrays are not packed data structures, they are arrays of references pointing to objects allocated elsewhere in memory.

I.e. there will be at least one level of indirection from the array to the object itself. This problem is often referred to as "pointer chasing".

I.e. usually the memory layout will look like this:

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

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

You can use jol to inspect the memory layout of objects.

The JDK devs are working on Value types as part of project valhalla that will eventually allow packed arrays to exist, which may be needed as part of project panama, but this still is far off into the future.

In the meantime there are 3rd-party projects aim to provide similar features:

Other projects either use off-heap storage (e.g. via sun.misc.Unsafe) or views on ByteBuffer / byte[] arrays to create packed, cache-friendly data structures at the expense of more complicated APIs.

1 Comment

wow thanks, so much usefull info :) didnt know about jol at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.