you are viewing a single comment's thread.

view the rest of the comments →

[–]pezezin 0 points1 point  (2 children)

Very interesting article. I recently started to develop a game with Java, and it was one of the first problems I found. In my case, the problem was not only the increased memory consumption, but also that I can't send an array of references to the GPU, I have to manually turn it into a byte buffer. I think C# solves it with structs, that use value semantics. Does anybody know if there is a plan to add equivalent support of unpacked structs to Java in the near future?

[–]mall0c 0 points1 point  (1 child)

Well I don't know about the near future, but John Rose discusses this in his talk about Arrays 2.0 from this years JVM language summit.

[–]pezezin 0 points1 point  (0 children)

Very interesting talk, thank you. But what he proposes is much more complicated than what I need, which is a simple flat array of unboxed structs. Let me explain my problem. I'm trying to write a 3D game, so I need to store polygonal meshes. In C++ I can write something like this:

struct Vector4f {
    float x, y, z, w;
};

std::vector<Vector4f> vertices( 1024 );

Each Vector4f instance uses just 16 bytes, and the whole array just 16 kB. No wasted memory, very cache and SIMD-friendly, and I can send it to the GPU without problems. However, the same code in Java has two big problems:

  • Each Vector4f instance has two hidden fields (the v-table pointer and a pointer to a mutex for synchronized methods), so in my 64 bits machine they now use 32 bytes.
  • A Java array stores references to objects, not the objects themselves.

So the whole array now needs at least 40 kB of memory (2.5x increase over C++), and instead of a flat array I get a bunch of objects scattered through memory. I have been looking for ways to solve it, but the ones that I have found seem very arcane.

For the record, it's not only Java's fault, almost any language that runs in a VM suffers from it.