Girardi
Girardi

Reputation: 2809

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:

Thanks in advance!

Upvotes: 7

Views: 891

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 660455

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.

Upvotes: 14

Cody Gray
Cody Gray

Reputation: 244951

The run-time just asks the operating system for more memory. The operating system handles paging the existing contents of memory out to disk and otherwise swapping things around to create more free physical memory, if necessary. The same thing happens here for managed C#/.NET programs as happens for regular, unmanaged programs.

The only difference in the world of managed applications is that the .NET run-time imposes some memory overhead, thus limiting the total amount of memory that is actually available for your application. For example, the garbage collector requires some memory space to do its work.

So yes, it's OS-dependent, at least to some extent. However, most modern operating systems have fairly similar approaches to memory management.

Upvotes: 3

Oded
Oded

Reputation: 499272

If you allocate more memory than physically exists, yes, swap space will be used.

If you allocated more memory than can be addressable, an OutOfMemoryException will occur.

Not sure about Mono, but I would guess that this is runtime dependent and would behave pretty much the same way (not enough physical memory will cause swapping, too much allocated would cause the exception).

Upvotes: 3

Related Questions