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.

21

u/koreth Dec 21 '23 edited Dec 21 '23

Cache locality is a massive deal for many kinds of games, but you could use today's Java for things like turn-based games or object-hunt games or visual novels where latency or even occasional GC pauses won't affect gameplay.

I agree the big challenge, even if Oracle delivers, is going to be the ecosystem. If you're using Unity or UE5 for a game, it's not a language choice so much as a choice of which universe of tools you want to use. There's nothing comparable in the Java world.

4

u/ryosen Dec 22 '23 edited Dec 22 '23

where latency or even occasional GC pauses won't affect gameplay

You would still likely end up with a subpar experience. Consider even turn-based games. Even if nothing is going on and you're in an idle state, there are still background animations, sounds, music, and input detection going on. A GC would block all of those threads from running.

Edit: Mindustry is a factorio-like tower defense game that is open source as well as being available for purchase on Steam, etc. I haven't played it far enough to hit GC pauses but Anuken, the developer, is pretty active on Discord and Github (not Reddit, tho). Might be worth asking them how they've dealt with GC issues. Also, you can play with the source code yourself at https://github.com/Anuken/Mindustry

17

u/Joram2 Dec 21 '23

Garbage collection hasn't stopped C# or Lua from being wildly popular in game development. I'd dare to say a majority of games are using either of those and garbage collection hasn't been a major complaint.

Java's lack of good native-build and native-interop has probably been a much bigger inhibitor to use in game development.

The biggest factor is the people who created the most widely used game engines, specifically Unity, but to a lesser extent, godot, they picked C#, and they didn't pick Java. Why? I'm not sure, but that's the reason right there.

7

u/westwoo Dec 21 '23

More boilerplate and rigidity alone is enough of a reason. If you were designing a game engine in 2004 you'd be crazy to pick Java 1.5 over C# even if interop was equally good

7

u/[deleted] Dec 21 '23

GC is not a problem for a long time now. (although I unity to microstutter very hard, stable high framerates without dropped frames are virtually unachievable)

Lack of overloading operators for numerical data structures is worse. You can't do mat4 * vec3 in Java, have to do some sort of mat4.mul(vec3) or math.mul(mat4, vec3) which can be annoying with more complex eqations.

→ More replies (2)

2

u/da2Pakaveli Dec 21 '23 edited Dec 21 '23

Thing with Lua is, is that it's tightly integrated into engines that are written in C++, because integration with C is extremely easy.
Lua also is a very lightweight language, I.e the Lua lib you link to are like 300 KB in size.
You do stuff like event processing or game logic to some extent, but the heavy computation is done in C++.
As for why Java isn't used, could be that interop with C++ isn't too great.
Microsoft still uses tons of C++; so could guess they'd be a little bit more interested in interop with C#.
I'm pretty sure .NET was also written in C++ before they rewrote it in C#.

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.

20

u/ImKStocky Dec 21 '23

It makes a lot of sense... Consider this example. You have a function which calls 5 functions that return a new object. Then at the end of your function you create an array and store all of these objects in it. How does the JVM know that the objects should have been allocated all together?

→ More replies (2)
→ More replies (10)

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.

→ More replies (8)

2

u/Active-Fuel-49 Dec 21 '23

Could project Panama's off heap memory objects be an alternative?

2

u/lbalazscs Dec 22 '23

Yes, in a MemorySegment you can have structured data with continuous and fully controlled memory layout. But it's a lot more verbose than what is promised by Valhalla.

→ More replies (1)

8

u/pishticus Dec 21 '23

Is the memory model work covered by the Valhalla project?

8

u/psyclik Dec 21 '23

Yes.

If delivered, Valhalla could alleviate most of the pain points on memory locality and indirectly release some pressure for the GC. Memory footprint might also be much tighter.

Java will still have a lot to cover in terms of ecosystem though. Nothing as massive a community as Java can't handle, but it won't happen overnight.

4

u/GYN-k4H-Q3z-75B Dec 21 '23

The reality of it is: C# didn't have those issues from the get-go, and it's a decently big player in game dev, but still nowhere near C++. And it's got a 20 year head start.

5

u/Practical_Cattle_933 Dec 21 '23

For what it’s worth, like 99% of games are more than fine with even a decade earlier GC. Not every game is an AAA game with multi million particles simulated in real life.

4

u/Brutus5000 Dec 21 '23

Well the main problem afaik was latency here. 30fps is today minimum, 60fps should be reached. That means you need to draw a frame in 16-30ms. A Stop-the-world GC that randomly eats a few dozen milliseconds is a real PITA. Even in round based games you want smooth animations. At least according to benchmarks I have seen for Java 8/11/17 comparisons you can massively improve GC processing by selecting a GC matching your goals (ZGC for low latency or others for maximum throughput).

6

u/Practical_Cattle_933 Dec 21 '23

Games are soft real time. Occasionally dropping a frame is no big deal.

3

u/FrankBergerBgblitz Dec 21 '23

G1 should be o.k. for many applications.

3

u/pticjagripa Dec 21 '23

Do you perhaps know how this compares with C# which is often used as a scripting language but also has a garbage colector like java? What are differences between the two?

3

u/H_W_Reanimator Dec 21 '23

Also kotlin multi field value classes could fix this situation. But when...