Hacker News new | ask | show | jobs
by matt_m 3689 days ago
The biggest thing compared to C and C++ (or even C# and Go) is probably the lack of user-defined value types. So if you want to create an array of Point2D objects, in Java it will be an array of pointers to heap-allocated objects (each with additional per-object overhead), which is really bad for cache misses, memory consumption, GC pressure etc. C# is a pretty similar language that is popular for game development (with Unity) but unlike Java it supports value types.
1 comments

I agree. It would be good with some kind of fly-weight allocation for a whole array of objects in java.

How is garbage collection for arrays of value objects solved? Is the whole array gc:d, or what? If an individual element is referenced somewhere the array stays in memory?

C# works a little differently from C/C++ in that stack vs heap allocation is decided at the type level (using a 'struct' or 'class' keyword when defining the type). So you can't easily have one object have a reference to an interior Point in a Point[] array, since any Point instance variable will just be copied by-value and not be a reference. A Point in an array can be passed by reference into a function using the 'ref' keyword though, in which case I'd expect the GC to keep the whole array around until the function returns? (It seems hypothetically possible to optimize but kind of a niche case). If you need to, you can also actually use raw C pointers with an 'unsafe' keyword, that's outside of the GC though (useful for working with C libraries).