• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# OutOfMemoryException类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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));
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:29,代码来源:OutOfMemoryException

输出:

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);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:OutOfMemoryException

输出:

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;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:36,代码来源:OutOfMemoryException

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:41,代码来源:OutOfMemoryException

输出:

Sample mean: 0.500022771458399, N = 200,000,000



注:本文中的System.OutOfMemoryException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# OverflowException类代码示例发布时间:2022-05-24
下一篇:
C# OperatingSystem类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap