[英]What's the easiest way to get an OutOfMemoryException in C#?
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. 结果太可怕了。
void Foo()
{
throw new OutOfMemoryException();
}
:))) :)))
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);
This does throw an OutOfMemoryException
: 这会抛出OutOfMemoryException
:
string s = " ";
while (true) s += s;
for(var s=" ";;s+=s);
var x = new long[Int32.MaxValue];
应该这样做。
你可以用这个:
new int[(ulong)-1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.