How do I force release memory occupied by MemoryStream?

后端 未结 4 699
北恋
北恋 2021-02-07 12:02

I have the following code:

const int bufferSize = 1024 * 1024;
var buffer = new byte[bufferSize];
for (int i = 0; i < 10; i++)
{
    const int writesCount = 4         


        
4条回答
  •  佛祖请我去吃肉
    2021-02-07 12:37

    Looks like you're allocating too much than your system can handle. Your code runs fine on my machine, but if I change it like this :

    const int bufferSize = 1024 * 1024 * 2;
    

    I get the same error as you.

    But if I change the target processor to x64, then the code runs, which seems logical as you can address lot more memory.

    Detailed explanation on this article : http://www.guylangston.net/blog/Article/MaxMemory And some information on this question : Maximum Memory a .NET process can allocate

提交回复
热议问题