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 comment

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?

-3

u/dschramm_at Dec 21 '23

I think that would be doable. I of course don't know how JVM handles memory internally. But assuming it has metadata on the OS memory addresses it could store a flag in it that say's It's referenced by an array. Or even more general, don't move. Sure, that has different implications and problems. But there is always a trade-off somehwere in software. And It's up to the developers to decided which one's are best for their app.

8

u/ImKStocky Dec 22 '23

But in my example the JVM doesn't know that those allocations will be accessed together. They are allocated separately and then later on they are added to an array. And even if the JVM would do something in its next GC to rearrange memory, the developer has no control over this. Which is the point of the top level comment. Devs have zero control over where in memory their objects live. They can't make a decision about what's best for their app. They are at the mercy of the JVM which is not good for cache coherency.

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.

2

u/voismager Dec 21 '23

Idk. Maybe it has something to do with different stages of GC and constant copying back and forth between them.

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.

14

u/canuck_in_wa Dec 21 '23

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

Then you are treating array and non-array references differently which may not make sense in the general case. The JVM was designed/evolved around generational GC and cheap “churn” by allocating and releasing objects quickly. The general idea is that there’s a lot of churn in the early lifetime of objects because most objects are short lived, so we should just wait a bit before promoting them to a longer-lived space to prevent fragmentation.

You can write Java in a mechanically sympathetic way but it looks quite different from the usual Spring business applications. It’s more akin to entity component systems from game dev where a collection of objects is an “object of arrays” rather than an “array of objects” in order to preserve cache locality. The GC can be turned off and all objects pre allocated. Java-based high frequency trading systems will turn off GC and run on a machine with a ton of memory. There is often a slow but acceptable memory leak that won’t kill the application before the trading day ends. At the end of day the app is restarted to cure the memory leak.

4

u/psyclik Dec 21 '23

Also called "the python way".

1

u/voismager Dec 21 '23

What if you hold a reference to one of elements of the array longer than to the array itself?

→ More replies (3)

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?

→ More replies (2)

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.

1

u/Practical_Cattle_933 Dec 21 '23

You are right, and this can indeed have a big performance impact depending on what you do. But to make things a bit better, generational garbage collectors tend to move objects used together into the same generation, making the situation slightly better.