本文整理匯總了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