在C#中获取OutOfMemoryException的最简单方法是什么?

时间:2021-09-26 14:03:49

Just curious as to how I can get this error the easiest way.

只是好奇我怎么能以最简单的方式得到这个错误。

Once I was trying to create a file navigator and I was creating Image thumbnails; that turned out awful.

一旦我尝试创建文件导航器并且我正在创建图像缩略图;结果太可怕了。

6 个解决方案

#1


139  

void Foo()
{
   throw new OutOfMemoryException();
}

:)))

:)))

#2


5  

Create a very, very large string. Probably:

创建一个非常非常大的字符串。大概:

string s = new string('a', int.MaxValue);

will be enough.

就足够了。

If not, you can concat it to build even bigger string.

如果没有,你可以连接它来构建更大的字符串。

string ss = string.Concat(s, s);

#3


4  

for(var s=" ";;s+=s);

#4


4  

This does throw an OutOfMemoryException:

这会抛出OutOfMemoryException:

string s = " ";
while (true) s += s;

#5


3  

var x = new long[Int32.MaxValue];

That should do it.

应该这样做。

#6


2  

You can use this:

你可以用这个:

new int[(ulong)-1];

#1


139  

void Foo()
{
   throw new OutOfMemoryException();
}

:)))

:)))

#2


5  

Create a very, very large string. Probably:

创建一个非常非常大的字符串。大概:

string s = new string('a', int.MaxValue);

will be enough.

就足够了。

If not, you can concat it to build even bigger string.

如果没有,你可以连接它来构建更大的字符串。

string ss = string.Concat(s, s);

#3


4  

for(var s=" ";;s+=s);

#4


4  

This does throw an OutOfMemoryException:

这会抛出OutOfMemoryException:

string s = " ";
while (true) s += s;

#5


3  

var x = new long[Int32.MaxValue];

That should do it.

应该这样做。

#6


2  

You can use this:

你可以用这个:

new int[(ulong)-1];