What happens when RAM runs out in C #?

I am not a computer expert, so let me try to ask this question a little more specifically:

I do some scientific calculations, and sometimes it takes a lot of memory to store their results. A few days ago I had an output file that took up 4 GB of hard disk space, but I have so much RAM. So:

  • How does the CLR (or is it something else?) Deal with memory when the program you run allocates more memory than is available on the computer? Does it create some swap in HD? (I know this may slow down my program, but I'm interested in a memory issue)
  • It depends on the OS, say, if I work with MONO on Linux or with VS on Windows?

Thanks in advance!

+6
source share
3 answers

The way I find it useful to think about is the memory on disk. RAM is a fast cache. Instead of thinking "when I exit RAM, the system will change it to disk," I think, "when I have available RAM, the system will move my memory to disk."

This is from how most people think about it, but I find it helps. RAM is just a performance optimization; the real limit on the amount of memory that you can allocate is the available disk space.

Of course, this is more complicated. On 32-bit operating systems, each process receives a user address space of 2 billion bytes. (And the same goes for the kernel address space, but ignore it.) Each page of memory that you can access, whether in RAM or on disk, must be in that address space. You can allocate more than 2 billion bytes, no problem. But you can only address 2 GB at a time. If you have 10 GB allocated, then at least 8 GB of it will not be displayed in the address space. In this case, you need to cancel something else, and then map what you want into the address space in order to get to it.

In addition, many things must be in the adjacent address space. For example, if you have a 1 MB stack, then there should be a million contiguous bytes in the address space.

When people have a "lack of memory" they do not have enough RAM; RAM is just a quick cache on disk. And they do not have enough disk space; there are a lot of things. They are almost always in situations where there is not enough satisfactory address space to satisfy demand.

The CLR memory manager does not implement these trendy map-and-unmap strategies for you; basically, you get a 2GB address space and it. If you want to do something fantastic, say with files with memory mapping, then you need to write code to manage the memory yourself.

+11
source

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

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

I'm not sure about Mono, but I would suggest that it depends on the execution time and will behave in the same way (insufficient physical memory can cause an exchange, too much allocation will lead to an exception).

+3
source

Runtime simply requests the operating system for more memory. The operating system handles the partitioning of existing memory contents into disk and, if necessary, swapping to create more free physical memory if necessary. The same thing happens for C # / managed programs. NET, as is the case for regular unmanaged programs.

The only difference in the world of managed applications is that the .NET runtime imposes some memory overhead, which limits the total amount of memory that is really available for your application. For example, a garbage collector requires some memory space to do its job.

So yes, it depends on the OS, at least to some extent. However, most modern operating systems have fairly similar approaches to memory management.

+3
source

Source: https://habr.com/ru/post/888110/


All Articles