How much memory does a MemoryStream use?

I have a 2 GB machine. Before running my exe, I have 1.1 GB of free memory. When I run exe, which is only one line, which you can find below, I get an exception in memory.

I expect the following line to use something around 600 million bytes. Does the MemoryStream class use more memory than initialized capacity?

MemoryStream memory = new MemoryStream(600000000); 
+4
source share
2 answers

According to the following Q & A, it is likely that this is because you EXE cannot allocate 600 megabytes of contiguous memory. (I tried to find something in the documents, this is the best I could come up with as soon as possible.)

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1af59645-cdef-46a9-9eb1-616661babf90

A โ€œmemory errorโ€ almost never occurs because theres not enough storage available; as we have seen, memory is disk space and disks are huge these days. Rather, a memory error occurs because the process cannot find a large enough portion of continuous unused pages in the virtual address space to perform the requested mapping.

Interestingly, this answer is related to Eric Lippert's post: http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory .aspx

+4
source

I found the documentation for backing up @SethFlowers suggestions (that the application cannot find a large enough contiguous block of memory):

When you initialize a new process, the runtime reserves an adjacent region of address space for the process. This reserved address space is called managed heap.

Automatic Memory Management (MSDN)

Here are some additional explanations for memory usage in .NET and how to profile application memory usage:

http://csharp.2000things.com/tag/virtual-memory/

0
source

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


All Articles