Virtual memory on PC

There is an excelent post about virtual memory. It’s written mainly from a perspective of console developer. On consoles most of memory issues are TLB misses and physical memory limit. I’ll try to write more about how (bad) it looks on PC (windows) with 32 bits programs. Especially nowadays when games require more and more data.

Firstly half of program’s virtual address space is taken by kernel. This means that first pointer’s bit is unused and it can be used for some evil trickery :). Moreover first and last 64kb are reserved by kernel.

Program’s source and heap has to be loaded somewhere. When compiling using VC++ default place is 0x0040000. Then a bunch of DLLs are loaded into strange virtual memory addresses. You can check what DLLs are loaded, into what address and see their size using Dependacy Walker. Use start profiling feature to see real virtual memory address of given DLL. DLLs and program usually aren’t loaded into one contiguous address range. At this point we didn’t call new/malloc even once and virtual memory is already fragmented.

Now there comes video driver. It will use precious virtual memory for managed resources, command buffer and temporary for locking non managed resources. Especially creating/locking non managed resources is quite misinforming as DirectX returns “out of video memory” instead of “out of virtual memory”. It’s very tempting to put all static level geometry into one 100mb non-managed vertex buffer. When creating/filling this VB video driver will try to allocate contiguous 100mb chunk of virtual memory. This will likely result in program crash after some time.

Windows uses 4kb pages, so doing smaller allocations will lead to internal fragmentation. I guess already everyone is using some kind of custom memory allocator, so it isn’t a problem.

There is /LARGEADDRESSAWARE linker flag, which allows to use additional 1gb of virtual memory. It requires user to change boot params and usually doesn’t work well in practice (system stability issues etc.). It’s also possible to compile as 64 bit program, but according to Steam HW survey half of gamers use a 32 bit OS. This is really annoying that MS is still making 32 bit systems because currently min PC game spec CPUs are core2 or similar with 64 bit support.

Summarizing in theory memory shouldn’t be a problem on PC, but in practice it’s a precious and fragile resource.

This entry was posted in PC. Bookmark the permalink.

One Response to Virtual memory on PC

  1. Anonymous says:

    I'm running into this very issue at the moment, and cannot even switch to 64-bit because it's an XNA game and the XNA assemblies are 32-bit only. Switching to another framework like SlimDX or SharpDX would be a painful. Makes me wonder why the XNA devs didn't release 64-bit assemblies. I wonder if it's marketing, or some technical limitations within the XNA framework… Oh well, I guess I will never know.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s