本文整理汇总了C#中System.OutOfMemoryException类的典型用法代码示例。如果您正苦于以下问题:C# OutOfMemoryException类的具体用法?C# OutOfMemoryException怎么用?C# OutOfMemoryException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OutOfMemoryException类属于System命名空间,在下文中一共展示了OutOfMemoryException类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
try {
// Outer block to handle any unexpected exceptions.
try {
string s = "This";
s = s.Insert(2, "is ");
// Throw an OutOfMemoryException exception.
throw new OutOfMemoryException();
}
catch (ArgumentException) {
Console.WriteLine("ArgumentException in String.Insert");
}
// Execute program logic.
}
catch (OutOfMemoryException e) {
Console.WriteLine("Terminating application unexpectedly...");
Environment.FailFast(String.Format("Out of Memory: {0}",
e.Message));
}
}
}
输出:
Terminating application unexpectedly...
示例2: Main
//引入命名空间
using System;
using System.Text;
public class Example
{
public static void Main()
{
StringBuilder sb = new StringBuilder(15, 15);
sb.Append("Substring #1 ");
try {
sb.Insert(0, "Substring #2 ", 1);
}
catch (OutOfMemoryException e) {
Console.WriteLine("Out of Memory: {0}", e.Message);
}
}
}
输出:
Out of Memory: Insufficient memory to continue the execution of the program.
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
Double[] values = GetData();
// Compute mean.
Console.WriteLine("Sample mean: {0}, N = {1}",
GetMean(values), values.Length);
}
private static Double[] GetData()
{
Random rnd = new Random();
List<Double> values = new List<Double>();
for (int ctr = 1; ctr <= 200000000; ctr++) {
values.Add(rnd.NextDouble());
if (ctr % 10000000 == 0)
Console.WriteLine("Retrieved {0:N0} items of data.",
ctr);
}
return values.ToArray();
}
private static Double GetMean(Double[] values)
{
Double sum = 0;
foreach (var value in values)
sum += value;
return sum / values.Length;
}
}
输出:
Retrieved 10,000,000 items of data. Retrieved 20,000,000 items of data. Retrieved 30,000,000 items of data. Retrieved 40,000,000 items of data. Retrieved 50,000,000 items of data. Retrieved 60,000,000 items of data. Retrieved 70,000,000 items of data. Retrieved 80,000,000 items of data. Retrieved 90,000,000 items of data. Retrieved 100,000,000 items of data. Retrieved 110,000,000 items of data. Retrieved 120,000,000 items of data. Retrieved 130,000,000 items of data. Unhandled Exception: OutOfMemoryException.
示例4: Main
//引入命名空间
using System;
using System.IO;
public class Example
{
public static void Main()
{
Tuple<Double, long> result = GetResult();
Console.WriteLine("Sample mean: {0}, N = {1:N0}",
result.Item1, result.Item2);
}
private static Tuple<Double, long> GetResult()
{
int chunkSize = 50000000;
int nToGet = 200000000;
Random rnd = new Random();
// FileStream fs = new FileStream(@".\data.bin", FileMode.Create);
// BinaryWriter bin = new BinaryWriter(fs);
// bin.Write((int)0);
int n = 0;
Double sum = 0;
for (int outer = 0;
outer <= ((int) Math.Ceiling(nToGet * 1.0 / chunkSize) - 1);
outer++) {
for (int inner = 0;
inner <= Math.Min(nToGet - n - 1, chunkSize - 1);
inner++) {
Double value = rnd.NextDouble();
sum += value;
n++;
// bin.Write(value);
}
}
// bin.Seek(0, SeekOrigin.Begin);
// bin.Write(n);
// bin.Close();
return new Tuple<Double, long>(sum/n, n);
}
}
输出:
Sample mean: 0.500022771458399, N = 200,000,000