当调用 StringBuilder.Insert 方法

OutOfMemoryException 异常。

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);
      }
   }
}
// The example displays the following output:
//    Out of Memory: Insufficient m

可以执行以下任一操作来解决该错误:

  • Int32.MaxValue的。

  • StringBuilder 对象的任何扩展。

您的应用程序作为32位进程运行,确要更多的内存

若要解决此异常,请重新编译你的应用以面向64位平台。 

应用正在泄漏非托管资源

IDisposable 接口。

(某些类型还实现 Close 方法,该方法在函数中与 Dispose 方法相同。)如果已创建使用非托管资源的类型,请确保已实现 Dispose 模式,并在必要时提供终结器。 

你正在尝试在64位进程中创建一个大型数组

在 .NET Core 中,默认情况下会启用支持大于 2 GB 的数组。

在内存中使用非常大的数据集(如数组、集合或数据库数据集)

OutOfMemoryException 异常结果。

例如:

  • 处理大型表时,多个查询几乎始终比检索单个表中的所有数据,然后对其进行操作的效率更高。

  • 如果执行的是用户动态创建的查询,则应确保查询返回的记录数受到限制。

  • OutOfMemoryException 异常,则应将应用程序修改为使用子集中的数据,而不是一次处理全部数据。

OutOfMemoryException

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;
   }
}
// The example displays output like the following:
//    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.

OutOfMemoryException 异常。

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);
   }
}
// The example displays output like the following:
//    Sample mean: 0.500022771458399, N = 200,000,000

反复串联大型字符串

OutOfMemoryException 异常。

StringBuilder 实例转换为字符串。

将大量对象固定在内存中

OutOfMemoryException 异常。

  • 评估是否确实需要固定每个对象,

  • 确保每个对象尽快取消固定。

  • GCHandle.Free 方法调用来取消固定该内存。

OutOfMemoryException 异常:

  • 文本框
  • newarr
  • newobj

 

相关文章:

  • 2021-10-05
  • 2021-11-19
  • 2022-12-23
  • 2021-10-06
  • 2021-05-30
  • 2021-05-21
  • 2021-09-28
猜你喜欢
  • 2021-10-14
  • 2021-07-25
  • 2021-12-21
  • 2021-07-20
  • 2022-12-23
  • 2021-05-16
  • 2021-06-11
相关资源
相似解决方案