Garbage Collection

Hi.

I'm not a game programmer yet but I'm a programmer. I have been learning computer graphics (opengl) at school and while I was talking to one of the professor was referring to Java as the main problem is that the garbage collection does not permit it to be real time. I know that the .net framework which I use quite often has garbage collection as well. The problem with garbage collection is that you (the programmer) does not know when it will happen.

My question are 1) Is Xna going to have garbage collection and 2) if it will have garbage collection, wouldn't this make a video game slow

thanks

Francisco

[655 byte] By [IBLUES] at [2008-2-15]
# 1

1) Yes, since it's based on the .NET Framework, garbage collection is built in

2) Not necessarily. Most of the time you won't even notice it. The guys that did the port of Marble Blast Ultra I believe said they didn't notice any performance degradation, even though they quickly threw togther the C# version.

JimPerry at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 2
it would be a good thing to add to one of my videos, but maybe not till an advanced series garbage collection seemes to be somewhat a mystery to many people.
aka_Big_Wurm at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 3

Thank you for the reply.

If it does have garbage collection and they did not notice a performance problem, I wonder if this would be notice at a very demanding video game. In any event, I'm excited about XNA

I wonder what's the response from the XNA team about the performance of Garbage Collection. Understanding the importance of Garbage Collection, it would be nice (this may sound crazy) to be able to disable such feature. what does XNA Team thinks about the garbage collection

thanks

Francisco

IBLUES at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 4
Garbage collection isn't really the kind of thing you can disable: it's

very much core to how the whole CLR and all .NET languages work.

This is certainly going to be one of the important areas for people to

understand in order to get the maximum performance out of managed code,

but it is certainly possible to write great games in garbage collected

environments.

Ever played Unreal, or seen the trailers for Gears of War (which uses

the Unreal engine)? A lot of the code for Unreal Engine games is

written in a scripting language called UnrealScript. Yep, that is

garbage collected...

In fact most big games these days use some kind of dynamic scripting

language. At my previous job we used Lua, as do many other games

including World of Warcraft. So garbage collection and games absolutely

can work well together, in fact the need for garbage collection is

exactly why so many games are using this kind of language!

ShawnHargreaves at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 5

Fancisco,

A couple things of note:

  • Since the entire product is based on the .NET Framework, garbage collection is an obvious foe.
  • Garbage collection will almost surely have an impact on how your games perform. If you're allocating a ton of memory and you have variables falling out of scope everywhere, you're probably going to notice some slowdowns. On the other hand, if you're not really freeing much memory, you might be ok. It really depends on your game and how you've decided to implement things.
  • In my experience, opengl is much different from DirectX, especially the managed flavor. I wish you the best of luck though.

As for Java, its main problem with being "real time" has little to do with garbage collection. In fact, many would argue that garbage collection is comparable to explicit memory management depending on the gc model. You're right that you don't know when garbage collection will happen, but Java's issue is actually that the bytecode, similar to .NET's IL, is compiled on the fly, something referred to as just-in-time compiling.

Hope this answered some of your questions, and I hope to talk to you again once XNA game studio comes out!

EdAltorfer at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 6

Shawn Hargreaves wrote:
]A lot of the code for Unreal Engine games is written in a scripting language called UnrealScript. Yep, that is garbage collected...

Though not quite the same. Last I checked Unreal will clean up references to Actors (but not Objects - there are usually 100s of Actors in a level, but 1000s of Objects) at runtime whenever a significant number of deletions have accumulated (basically lazy smart pointers), but holds off doing a full garbage collection until level change time. This may have changed for Gears since they've added streaming support, but I imagine it's still quite a different beast than the CF collector.

One thing I'd love to see added to XNA is a little more control over the GC. I'd love the ability to pause GC during initialization (where a whole bunch of allocations are likely going on), then trigger a collection at the end before handing control over to the player.

I'm still curious how the "keep your heap use around the size of the L2 cache" (i.e. 1MB) will play out in practice. I'm assuming this includes both those static long lived objects and the shorter lived pointery frothy goodness. What's our baseline after XNA itself is all initialized? What tools are available for tracking this stuff in the first place?

AaronLeiby at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 7
I went to a session at Gamefest where the guys from GarageGames (who converted their Torque engine to use managed code and XNA) talked about this exact issue. Garbage collection was a problem for them but not one they weren't able overcome. Developing in the .NET environment does require that you learn more about garbage collection and how it works in the managed code environment. They came from a C++ background, so they had a lot to learn about managed code and C#.

But basically, once they started paying attention to garbage collection and how it worked and creating and destroying objects and using the appropriate types of objects, the unexpected, uncontrollable garbage collection did not give any hiccups or stutters to their game.

Garbage collection is not something you can ignore and you do have to program intelligently for it if you have an intensive game, but it's also not something that will prevent you from making an intensive game using XNA.

I'm sure as people get started in XNA development (tomorrow!), they will begin to post the recommended do's and dont's of garbage collection for game development.

GeorgeClingerman at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 8
It's amazing the posting that this question had. I think is a very good topic.
Like I said, I'm looking forward to XNA.
IBLUES at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 9

There is information about the XNA Garbage Collector over that Rico Mariani's Performance Tidbits. He says avoid medium lifespan objects and pointers. Instead of pointers he suggests handles (whatever they are).

As a managed code programmer I can tell you that the GC is your friend. A lot of unmanaged programmers are going to demand fine control over the GC and this is unnecessary. XNA is using a very efficient GC (to space copying) and it is probably going to be another one of those cases where you shouldn't concern yourself unless you have a problem. I know that sounds wrong because the key is always in the detail, but most of the problems with GCs and memory management in general are solved simply because C# is a strongly typed language.

Blisardo at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...