Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when the RAM is over in C#?

I'm no computer expert, so let me try to put this question a little bit more specifically:

I do some scientific computations, and the calculations sometimes requires a lot of memory to store their results. A few days ago, I'd an output file that took 4 GB in hard disk, but I have this amount of RAM. So:

  • How does the CLR (or is it something else?) deals with the memory when the program you're running allocates more memory than that available in the computer? Does it create some swap in the HD? (I know that could slow down my program, but I'm only interest in the memory issue)
  • Is it OS-dependent, say if I work with MONO on linux or with VS on Windows?

Thanks in advance!

like image 371
Girardi Avatar asked May 14 '11 13:05

Girardi


1 Answers

The way I find helpful to think about it is: memory is disk space. RAM is a fast cache. Rather than thinking "when I'm out of RAM, the system will swap it to disk", I think "when I have available RAM, the system will move my on-disk memory into it".

That's backwards from how most people think about it, but I find it helps. RAM is just a performance optimization; the real limit on how much memory you can allocate is available disk space.

Of course it is more complicated than that. On 32 bit operating systems every process gets a 2 billion byte user address space. (And the same for the kernel address space, but let's ignore that.) Every page of memory you can access, whether it is in RAM or on disk, has to be in that address space. You can have more than 2 billion bytes allocated, no problem. But you can only address 2 GB of it at a time. If you have 10 GB allocated, then at least 8GB of it will not be mapped into address space. In that case you have to unmap something else and then map what you want into the address space in order to get at it.

Moreover, lots of things need to be in contiguous address space. If you have a 1MB stack, for example, then there needs to be a million contiguous bytes available in the address space.

When people "run out of memory" they are not running out of RAM; RAM is just a fast cache over the disk. And they are not running out of disk space; there's plenty of that. They're almost always in a situation where there is insufficient contiguous address space to meet the demand.

The CLR memory manager does not implement these fancy map-and-unmap strategies for you; basically, you get your 2GB address space and that's it. If you want to do something fancy, say with memory mapped files, that's up to you to write the code to manage the memory yourself.

like image 51
Eric Lippert Avatar answered Oct 18 '22 10:10

Eric Lippert