简体   繁体   English

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

[英]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.

相关问题 在C#中从bool转换为字符串的最简单方法是什么? - What's the easiest way to convert from a bool to string in C#? 使用C#更改字符串中文本内容的最简单方法是什么? - What's the easiest way to change the contents of text in a string with C#? 用C#在两个进程之间进行通信的最简单方法是什么? - What's the easiest way to communicate between two processes in C#? 使用C#创建Excel表的最简单方法是什么? - What's the easiest way to create an Excel table with C#? 在c#中加密文件最简单的方法是什么? - What's the easiest way to encrypt a file in c#? 在 C# 中创建托管可视化工具的最简单方法是什么? - What's the easiest way to create a managed visualiser in C#? 在 C# 中减去时间的最简单方法是什么? - What is the easiest way to subtract time in C#? 使用C#获取OpenGL版本的最简单方法 - Easiest way to get the OpenGL Version with C# C# 控制台应用程序(游戏后端):最简单(但也可扩展)的托管方式是什么? - C# Console Application (game back-end): What's the easiest (but also scalable) way to host it? 在C#中将整数列表转换为逗号分隔数字串的最简单方法是什么? - What's the easiest way to convert a list of integers to a string of comma separated numbers in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM