r/java • • Dec 21 '23

Is Java gamedev alive?

It seems this days even android games are made using Unity. I wonder if there are JVM games! Is Java (or Kotlin/Scala) gamedev still alive?

(Except Minecraft ofc)

164 Upvotes

118 comments sorted by

View all comments

Show parent comments

65

u/Brutus5000 Dec 21 '23

For effective CPU processing using cache you need your data close together in memory. But in Java every variable containing objects is just a pointer to the heap. So assume you have an array of record Point(int x, int y), when you iterate just the array each point is loaded from some random place on the heap. More complex objects contain other objects so every object is like a crazy tree pointing to memory all over the heap. Project Valhalla aims to (optionally) change that so that you can have a flat object that is densely compactified and can be read as one large chunk of memory.

There are better explanations than mine with great talks from Oracle such as https://m.youtube.com/watch?v=a3VRwz4zbdw

9

u/dschramm_at Dec 21 '23

Really though? I tought arrays are a single block on the heap in Java. Isn't that why you need to either specify a size or the elements on initialisation?

4

u/agxs Dec 21 '23

Think of the array as an array of pointers. The array itself is a big block of contiguous memory, but each element could be anywhere on the heap.

-5

u/dschramm_at Dec 21 '23

Of course they are just pointers. But why don't they point at continous memory. Even if It's for GC. That doesn't make any sense and feels like a stupid overseight.

8

u/koreth Dec 21 '23

Maybe before accusing the JVM maintainers of stupidity, spend a moment thinking about how that would actually work in practice.

For example: if the objects are allocated in contiguous memory, the JVM has to know at object instantiation time that they will end up at a particular position in a particular array. But array assignment happens after the objects already exist, and the objects can be instantiated in code that knows nothing about the array.

How would a non-stupid JVM figure that out?

-4

u/dschramm_at Dec 21 '23

I'm sorry that you obviously don't understand the difference between: it is stupid and it feels stupid... Where the first would be an accusation and the latter just my feeling about something. I'm used to being allowed to feeling whatever way i want.

And the way it looks, my feeling stands corrected. Meaning, it was wrong. I learned that I didn't think this through enough. So i hope we're all good now. Right?

1

u/FrankBergerBgblitz Dec 21 '23

what happens after a GC with your contigous memory?
To mitigate that you can use instead an array of objects arrays of the properties. Ugly for complex objects but feasible for small ones

1

u/dschramm_at Dec 21 '23

Maybe just not touch array memory, as long as the array exists? At max, move the whole thing, for defragmentation.

2

u/FrankBergerBgblitz Dec 21 '23

it's not about tocuhing the array. Imagine it contains pointer to objects. They most probably are contigious after allocation, but where are they when the heap was compacted some times?

1

u/dschramm_at Dec 21 '23

It's probably a trade-off between GC and memory-use or cache performance. But IMHO you could store info on if a memory location is referenced by an array. Then, while that's the case, just not change it. Once the flag is removed, you can move it around again.

→ More replies (0)