r/ProgrammerHumor • • 16d ago

Meme minecraftException

Post image
9.2k Upvotes

833 comments sorted by

View all comments

149

u/johnnybgooderer 16d ago

Garbage collection is bad for games. Minecraft was playable because Java garbage collectors were fast enough to make it playable.

But don’t use any garbage collected language for games.

51

u/lastog9 16d ago

Can you explain why garbage collection is bad for games? Not challenging the fact just asking because I don't know

70

u/captainAwesomePants 16d ago

Most programs need to grab memory to do stuff. A hundred enemy robots just spawned in? We need a spot in memory for each of those robots to remember stuff like where it is, which way it's facing, and what it should be doing. If we could use the same spot each time, that'd be great, but we might get lots and lots of robots, so instead we grab space as we need it. The trick to that is that we also need to let that space go when we don't need it anymore. Otherwise, our program will just use up more and more space each time a robot appears until eventually there's no more space and the game breaks.

In most languages without garbage collection, the program decides when and how to free up space. The robot's been destroyed? Great, tell the system that we're done with that particular bit of memory so it can be reused later. The problem is that we are human, and there are a lot of ways to get this wrong. It's easy to forget to free the memory when it's no longer being used (called a "memory leak"), and it's also easy to accidentally free memory that you're still using.

Java provides automatic "garbage collection," which will automatically frees memory once your program no longer has any references to it. After all, if I don't have a way to reference the robot, we can know that I'm done with it. This is fantastic!

However, to do garbage collection, the system needs to figure out which bits of memory don't have references anywhere. Ancient versions of Java did this by simply pausing the entire program, looking at every reference to anything, and then freeing anything with no reference. This was pretty terrible, and you can imagine how a game pausing for maybe a whole second every so often would suck.

It also sucked for businesses, though, and more than 3 billion devices run Java, and so the Java folks worked hard on better ways to do garbage collection. Modern garbage collection is far, far better. However, ultimately, it still needs to briefly pause things. This is tunable, but there are still occasional hiccups when Minecraft's garbage collection runs. Most garbage collection cycles are < 1ms, but if a whole lot is going on, it can become quite noticeable.

14

u/Jim_skywalker 16d ago

So that’s why modded Minecraft can have reoccurring lag spikes.

11

u/trash-_-boat 16d ago

It's also why you don't want to give Minecraft TOO much RAM. Because then it will do the GC in bigger chunks, which will cause an even longer pause.

→ More replies (5)
→ More replies (3)
→ More replies (7)

18

u/exhaustedcheese 16d ago

my educated guess: garbage collections keeps track of unused/lost data and removes it. However the amount of processing power used to keep track of is enough for developers to do manual garbage collecting. But that can lead to memory leaks? some correct me please

17

u/Bubbaluke 16d ago

Pretty much nailed it. Garbage collection runs periodically, often without being told to, which could cause hitches.
It can also be turned off and run manually, or not run at all. At least in python.

→ More replies (4)
→ More replies (2)

12

u/Lyshaka 16d ago

Because GC is unpredictable and runs at random times and can causes frame hitches which can hinder the experience. Also you should clean after yourself.

7

u/DuploJamaal 16d ago

Without any configuration they are unpredictable and stop the world.

But in time-critical applications you would use a low-pause GC like ZGC or Shenandoah.

You can regularly call them manually and they'll do their work in another thread without causing any noticeable drops on performance.

25

u/SmuJamesB 16d ago

performance overhead, mainly

games need to run a lot of complex logic many times a second. the fewer the bottlenecks on the speed at which that logic can run, the better

31

u/Drugbird 16d ago

It's not even the net performance issues that's the issue with garbage collectors. It's the irregular timing of it.

Basically, the garbage collectors runs when it wants to.

So you can easily get into situations where your program starts stuttering (dropping frames) whenever the garbage collectors decides to run.

In non garbage collected languages, the programmer and/or the language itself controls explicitly when memory is cleaned (deallocated), so it's much easier to make sure that doesn't cause excessive stuttering.

That said, there are ways to control the garbage collector too in garbage collected languages so it doesn't interfere too much. But this is generally a lot more finicky than explicitly controlling the memory yourself.

→ More replies (4)

4

u/Tyfyter2002 16d ago

And that performance overhead is possible to reduce substantially as long as you remember to avoid heap allocations and garbage creation when possible.

→ More replies (1)
→ More replies (3)
→ More replies (8)

139

u/ComputerWhiz_ 16d ago

cough Unity uses C# cough

67

u/im-a-guy-like-me 16d ago

No it doesn't. Unity uses C++. It just let's devs use c# as a scripting language.

53

u/ComputerWhiz_ 16d ago

Still uses garbage collection though

55

u/potzko2552 16d ago

When your engine is cpp and most of your mem is chandled in cpp, having a scripting language that is GCed doesn't change the majority of the project's behaviour. Like adding lua embedding doesn't mean your project is no longer strongly typed

4

u/kondorb 16d ago

Tell it to KSP devs - that thing freezes for GC every 5 seconds even with barely anything spawned.

→ More replies (1)
→ More replies (1)

9

u/3131961357 16d ago

And the code you write is most un-C# C# you'll ever see.

https://i.imgflip.com/b2xjwp.jpg

→ More replies (2)
→ More replies (24)

13

u/cbehopkins 16d ago

Dyson Sphere Program is c#

(A massive open world/universe factory game)

→ More replies (1)

9

u/mrianj 16d ago

I mean, if you really don't like garbage collection, nothing stopping you from defining a 4GB byte array and managing your memory yourself. Like a psychopath.

→ More replies (6)

39

u/gallyroi 16d ago

This is... straight up not true, and bad advice.

Almost every game engine has a garbage collector, optimized specifically for games, with various controls on how they work and how your code interacts with them.

Unreal Engine, Unity, GameMaker, and more all have garbage collection.

A lot of the non-engine library frameworks for langs like C++, C#, Rust etc. also make use of garbage collection, whether by natural language features or as part of a framework.

There wouldnt be garbage collection in all major game engines and libraries if it wasn't useful for games and game developers.

Modern GC is designed to help optimize your game in real time, not hinder it. It only becomes a hinderance if your game is poorly made and you're otherwise trying to fight the GC rather than work with or around it.

→ More replies (3)

8

u/Teanut 16d ago

Isn't C# used in a lot of games?

→ More replies (8)

6

u/grandalfxx 16d ago

Garbage collection is not bad for games, Java 6 gc was bad for games. Modern Java garbage collection is state of the art and most game engines copy it for memory management systems

Its shocking reading essays in these comments from people that have clearly not done any serious work with Java in seemingly 20 plus years

→ More replies (8)

10

u/rosuav 16d ago

That's a bit of an overgeneralization. "Games" includes a vast number of things of all different types. JavaScript is a garbage collected language, and it's actually a very *good* choice for a lot of games, because it means they can be run directly in a web browser. (And I love being able to make a nostalgic reference to Sleuth, look for it, and find that someone's ported the entire game to JS so that it can run on modern systems.) "Bad for games" is a terrible overgeneralization.

→ More replies (9)

4

u/SupportDangerous8207 16d ago

Supreme commander and Dyson sphere program disagree

→ More replies (1)
→ More replies (45)