XNA/Xbox 360 memory management heads-up...

I was just browsing through some docs on the 360's heap manager, and it turns out it is a compacting mark and sweep collector rather than the generational model used on the desktop...

So... what's the deal regarding:

  • Large block allocation (will it have a large object heap like the desktop version?)
  • GC pauses times (do all threads halt during GC)?
Rico Mariani recommends a heap size equivalent to the Gen 0 size on the desktop, i.e. ~cache size. That seems way too small unless we are dumping lots of game data into unmanaged memory!

Andy.

[577 byte] By [AndyL] at [2008-2-10]
# 1
Which 360 docs are you referring to? The XNA team has not released any documentation regarding how memory is managed on the 360.
DavidWeller-MSFT at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 2
Rico Mariani's last blog entry.
RalfKornmann at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 3
Maybe docs was the wrong word ... anyhow it was: http://blogs.msdn.com/ricom/archive/2006/08/22/713396.aspx

I guess a lot of it is based upon the notion that the 360 runtime is based upon the .Net CF, but anyhow, the blog claims to be regurgitating the contents of his gamefest talk.

The original question might seem a little premature, but any development that takes place on the beta will have to be based upon this knowledge (structs vs classes, heap size and allocation profiles). Sadly, if the 360's memory manager is really mark and sweep compacting, this means the best coding strategy will differ between it and the PC.

Andy.

AndyL at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 4
Hmmm, on reflection I really can't believe that a compacting GC would be any use for games development, but Rico is pretty reliable and he has published info (see above) to imply this is the case.

Could someone in the know clarify this?

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

Hi Andy.

We are doing some investigation work with Rico around clarifying these issues. We'll post more details when we get them.

Thanks.

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

Rico Mariani's main suggestion for dealing with the compacting garbage collector is essentially to try to make a large chunk of your data as static as possible and to use "handles" (eg. integer indexes) within that data to refer to objects as much as possible. Having most, if not all, of a game's data statically rather than dynamically allocated was pretty standard back in the days when a PC's memory was limitted and you couldn't depend on virtual memory to handle accidentally over-committing memory. On the consoles that was even more true. If you can keep your dynamic allocations to a small "froth" above a mostly static set of monolithic data allocations containing few pointer references then you should be able keep the cost of garbage collection to something comparable with the generational collector. It should also be a win with the generational garbarge collector, though not enough to make it worth the trouble if you don't have to support the Compact Framework.

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

I fully understand what you are saying, but why on earth implement a compacting GC on a device with 6 hardware threads and 512Mb of RAM? Messing around with block allocations negates all of the productivity benefit of managed memory, and I suspect using the handles will result in a huge array bounds check penalty, unless you do everything with unsafe C# pointers (in which case I might as well have called malloc and used C). I doubt you'll even be allowed unsafe code blocks on the 360 due to the security implications.

Lets see what the guys at MS have to say ...

Andy.

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

AndyL wrote:
I doubt you'll even be allowed unsafe code blocks on the 360 due to the security implications.

You're correct. 360 games using GSE will exist in their own little box with only safe access to outside resources.

JimPerry at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 9
We actually do allow C# unsafe code blocks on Xbox, including arbitrary

pointer arithmetic. You just don't have any way to P/Invoke into native

code from the managed sandbox.

Regarding heap sizes, one thing to bear in mind is that the biggest

data in a game tends to be textures, vertex data, and sound waveforms.

These are GPU resources, and don't count towards Rico's managed heap

size (other than a tiny little managed resource header).

Even looking at AAA commercial games, it is surprising just how small

their working data set can be if you look only at the active CPU

objects, not counting all the GPU data!

ShawnHargreaves at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 10
Hmm, that's news to me about the unsafe code. Good to know though.
JimPerry at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 11

The obvious answer is that they didn't implement a garbage collector for the XBox 360, they're just using the one from .NET Compact Framework as is. I don't think the array bound check penalty will be a problem for most games using the XNA Framework, it's just going to be an additional constant and predictable overhead of using managed code. This is different from the penalty caused by garbage collection which has the potential to cause stuttering and poor responsiveness even in games with low performance demands.

RossRidge at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 12
You're right about the sandboxing, just not about the details of how

that works. It turned out that it was possible for us to support unsafe

code and pointers even inside a secure sandbox on Xbox!

ShawnHargreaves at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 13
The desktop garbage collector DOES use mark and sweep compaction. It keeps track of generations in order to only mark and sweep the youngest generation first. It only goes on to process the older generations if it didn't free up enough space with the younger ones.

Also, "Array bound check penalties" have not been a problem for managed code since the introduction of the IE4 java virtual machine. (Java is not C# of course but the technology is the basically the same.)

Many common cases don't require array bounds checking at all in the inner loop. For instance:

int[] array = GetMyIntArray();
for (int i = 0; i < array.Length; i++)
array[ i ] *= 4;

Does the code above need to check array bounds at all? No, it's guaranteed not to exceed the bounds and a smart jit compiler knows this.

Array bounds checking and other similar things WILL NOT slow down your game... writing too many vertexes and/or failing to cull geometry WILL slow down your game.

Jurak at 2007-8-31 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 14
Another thing springs to my mind in respect to garbage collections...

Beloved XBox360 has a unified memory architecture, right?
So we can surely create any kind of buffer or texture via a direct (somehow aligned) memory pointer, right?
Maybe we can set the base of a buffer on the fly to another location?

I think about pinning managed memory...
Or do we have to use the DX9 (Windows) style, and have to lock and copy? This influences engine design largely...

Or maybe I am completely wrong here?

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