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

105

u/Brutus5000 Dec 21 '23

There are two things that blocked serious game development for more than a decade: garbage collector which freezed the application and a memory model that works as an anti pattern to how modern cpu cache architectures work. Garbage collection has massively improved in the last 10 years. Improving the memory model is heavily worked on by Oracle for multiple years, but still work in progress.

So there is a tiny ecosystem that can't compare with the hundreds of millions of dollars that went into C++ & friends ecosystems.

It might get interesting again in a few years though if Oracle delivers what they are promising.

32

u/m39583 Dec 21 '23

Could you expand on the memory model? I'd be interested how they conflict!

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?

29

u/voismager Dec 21 '23

They are, but they contain pointers. Actual data could be (if it's not primitives) somewhere else.

-13

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.

6

u/[deleted] Dec 21 '23 edited Dec 21 '23

it frequently does but the way HotSpot works is pack and sweep. The memory allocation and garbage collection is expensive, so it mallocs relatively huge blocks of memory at a time. it won't trigger garbage collection or a new memory allocation until it can't find space for it within that block. when it "allocates" memory for a new variable most of the time it's really just picking where in the current block of pre allocated memory that new variable should go. if it doesn't have space it will garbage collect and "sweep" over the currently allocated memory marking blocks that are no longer being used as available

only if it's been garbage collected recently and there's still not space does it go out to the os for a new malloc. the allocation and especially the overhead of garbage collection is postponed until absolutely necessary because it's expensive. but the consequence is you get non deterministic pauses for garbage collection and your location in the heap is wherever hotspot finds space for it. it may be contiguous it may not

1

u/dschramm_at Dec 21 '23

Thanks for the nice overview lesson. Really!

As I mentioned somewhere else, with GC, you have to make a trade-off somewhere IMO. Either you use more intelligent/slower GC using more metadata to get efficient memory locations for the least cycles and transfers in the app. Or you do dumber/faster GC with less metadata. What you do, probably depends on your hardware limitations and what your app does.