Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Pretty much all resources relating to game programming, especially 3D open-world games, talk about how you have to constantly be unloading and reloading assets to and from disk, system memory, and video memory. I can understand this on consoles, as they have very simple memory management schemes that cannot deal with any potential overflow.

However, on the PC, the situation is very different. There is virtual memory provided by the OS for system memory and the graphics driver handles swapping back and forth from the CPU to the GPU. So why is it that it's not sufficient to load everything at once and only have to deal with prefetching and possibly making sure that there is no blocking while a required asset is being swapped?


This is not to say that everything is being allocated naively using something like malloc. Everything will be kept contiguous and cache-coherent. It's arguably easier to make your memory access efficient if you don't have to worry about paging in and out manually anyways...


UPDATE:

After reading a bit into asynchronous buffer transfers, I get the idea that the way the graphics driver can automatically page memory in and out is suboptimal. Does this also hold true for CPU resources; i.e., is it better to always manually manage when a given resource is to be loaded and unloaded even in the presence of virtual memory?

share|improve this question
1  
You are assuming that asset content is stored in system memory. Much of your asset data will be stored in GPU memory, which is much more limited. –  dadoo Games Mar 25 '14 at 20:25
    
From reading the answer here I get the idea that OpenGL drivers automatically keep all buffers/textures on the CPU as well. However, I can see why it might be useful to specify these transfers manually to take advantage of asynchronous uploads. –  Boreal Mar 25 '14 at 20:46
    
Are you sure this is relevant to your game project? I am not sure I see the technical challenge, only the solution. If you are not facing a technical challenge, it is hard to tailor a solution or understand one. –  zehelvion Mar 25 '14 at 20:55
    
I'm wondering why game engines implement complex asset caching systems when operating systmems/drivers already have virtual memory systems available. It is relevant to me because I'm writing an open-world game where this information is very important. –  Boreal Mar 25 '14 at 20:59
    
Is your game suffering from performance issues and the profiling tool is showing it's because of memory access? The issue with games that contain open levels is that in technical terms, this translates to a lot of data. The more control you have on that data, the better the performance will be. Why not implement the most efficient system to manage data? Game development is very competitive and when you have money for experts, you aren't going to leave any stone unturned, looking to squeeze out more detail and higher fps. –  zehelvion Mar 25 '14 at 21:00

2 Answers 2

up vote 16 down vote accepted
+50

Modern open-world games simply don't fit in memory. Keep in mind that most games are still 32-bit due to the number of gamers with 32-bit OSes, and a 32-bit process at best can only have 4GB of addressed memory at a time (independent of virtual memory) but realistically is limited to 2-3 GB. Toss in fragmentation and the actual amount of usable objects you can have in a memory at a time is a fair bit smaller than the total data you need for a large environment. Even on a 64-bit OS, you often need to target low-spec machines with only 4GB of memory to ensure maximum end-user compatibility. Let's not forget mobile devices, "under-powered" hardware like the WiiU, etc. Developers have to target a lot more than big fancy gaming PC rigs.

Virtual memory is not a solution, firstly because it doesn't negate address space limitations and also because it is not optimal. The OS can page things out at inopportune times or in inopportune ways. This was a problem dealt with by OSes when hibernation first came around; paging out the processes to disk and then paging them back in was significantly slower in some cases than having the application explicitly stream the resource back in. You can still see the problems today when a system is running out of memory, pages are paged out to disk, and the system becomes unresponsive becomes critical things necessary to run your WM/explorer.exe get paged out. Virtual memory was a solution devised to a problem decades ago where systems had very little RAM and the gap between memory and disk speeds was much smaller. Even today's fastest SSDs are a tiny fraction of the speed of cheap/slow RAM, and trying to pretend that you can use your disk as virtual RAM just isn't the best idea anymore. This is an age where some people do the opposite and load up file systems into RAM disks; the constraints that led to the creation of virtual memory don't hold true anymore.

Even aside from disk issue, custom resource management is still a big part of game development. From debugging hooks to fragmentation concerns to intelligent streaming of complex resources, simply pretending the whole world is an infinitely big box of bits is not realistic.

share|improve this answer
    
Thanks for the detailed answer. Although I'm still looking for some information about the way GPU drivers handle virtual memory (between VRAM and system RAM). –  Boreal Mar 25 '14 at 22:00
    
@Boreal: same issues. Just replace "RAM" with "VRAM" and "disk" with "CPU memory –  Sean Middleditch Mar 25 '14 at 22:15
    
@SeanMiddleditch +1 Nice answer, "This is an age where some people do the opposite and load up file systems into RAM disks" do you mean memory mapped files? Also what I understand from your answer is that custom memory allocator prevent the OS from paging-out the memory, isn't this the case as memory will be paged as far as you ask memory from the kernel using brk() for example, how paging on PC is exactly prevented using a custom memory allocator? –  concept3d Mar 26 '14 at 0:00
1  
@concept3d: not memory mapping, but literal RAM disks. You can make a drive that is backed by memory and not the disk. The custom memory manager doesn't prevent paging (some other app could be using up all memory). Managing resources intelligently (at the object/resource management layer) simply means that you free up memory for objects you don't need anymore. Think of the OS's disk caches it keeps in memory –  Sean Middleditch Mar 27 '14 at 6:05

For a 32-bit game, as most games are for a variety of reasons, even a game that comes on one single-sided DVD (4.3GB) already has far more content that can be fitted into a 32-bit address space. And that's assuming the content isn't compressed on disc, and a perfectly optimal, load everything at once into contiguous address space approach. Many games now come on multiple DVDs, easily surpassing 10GB of content. In my experience on PC, once your address space approaches 2GB allocations will start to fail, and that becomes a hard limit, regardless of how much physical memory the user has.

If you do make the jump to 64-bit, assuming your engine supports it, that hard limit goes away, only to be replaced by an effective soft limit - as soon as virtual memory swapping begins to happen, the performance of the game will drop unacceptably. If the main process ever has to wait for memory to be paged back in, the frame-rate will stutter and glitch. Virtual memory might make that transparent, but it doesn't make it performant. It physically takes time to get that memory back from disk, and while you are waiting, your game has stalled. While it's possible to work around (with background pre-fetching of memory pages that you know you'll need soon), it's a difficult and unreliable process, and will still have performance implications.

The OS's algorithms for deciding what pages to swap out to disk have no knowledge of your game and what it is likely to need next. Furthermore, the OS still has to support other applications running simultaneously with your game, as well as the OS itself. Having to stall while pages are returned from disk is a massive performance hit, and it doesn't matter whether it's your game, another application or the OS that incurs it, the game's performance will suffer massively.

So regardless of whether the memory limit is soft or hard, memory is still limited. But even if you could just load everything at once, without hitting a limit of performance, there are still reasons why you wouldn't want to:

  1. Your game takes far longer to load and get to an interactive state than it needs to, because it simply loads everything at the start and keeps it resident. You can load about 150MB/sec from a hard disk, and even the fastest DVD drive (24x) loads at 33MB/sec. That means the example 4.3GB DVD worth of content will take at least 30 seconds to load from HDD, and over 133 seconds to load from DVD. Those are unacceptably long load times.

  2. Your game takes much more memory than is justified. If only 10% of the content is visible at any one time, holding the rest resident is simply wasteful, and prevents the user from using that memory for something else. Users are generally unforgiving of games which require a disproportionately large amount of memory to run properly.

Streaming assets from disk dynamically, whether in the background or synchronously during a loading screen, allows you to be far more efficient in your use of memory, with negligible effort and only a minor downside for the user. But it's an optimisation, and like any other optimisation it has diminishing returns the more you try to apply it. At some point the increase in memory footprint for keeping something resident is outweighed by the advantages gained from having it there without the need to load. But until that point is reached, it's better to load only what you need, when you need it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.