【问题标题】:Exception of type 'System.OutOfMemoryException' was thrown while reading data from text file从文本文件读取数据时引发“System.OutOfMemoryException”类型的异常
【发布时间】:2015-09-01 18:16:41
【问题描述】:

我正在尝试使用“|”读取文本文件数据分开,我使用下面的代码。尝试将数据表数据放入数据视图时,我能够在读取后从文本文件中读取数据我收到 'System.OutOfMemoryException' 类型的异常,

谁能建议我如何避免这个异常。

string filepath = System.Configuration.ConfigurationManager.AppSettings["data"];  

if (filepath != "")
            {
                DataTable dt = new DataTable("file");
                string[] columns1 = null;
                var lines = File.ReadAllLines(filepath);
                int Count = lines.Length;
                //here taking columns and adding to table
                 if (lines.Count() > 0)
                {
                    columns1 = lines[0].Split(new char[] { '|' });
                    foreach (var column in columns1)
                        dt.Columns.Add(column);
                }                   
                 for (int i = 1; i < lines.Count(); i++)
                {
                    DataRow dr = dt.NewRow();
                    string[] values = lines[i].Split(new char[] { '|'  });
                    for (int j = 0; j < values.Count() && j < columns1.Count(); j++)
                    {
                      dr[j] = values[j];
                    }

                }
                 dt.Rows.Add(dr);   

                    DataView View = new DataView(dt);
                    //Here I m getting "Exception of type 'System.OutOfMemoryException' was thrown."
                    DataTable MD = View.ToTable("MD", false, "ID", "Description")
                    DataTable MM = View.ToTable("MM", false, "RecordNumber", "Item description")
                if (MD.Rows.Count > 0)
                {
                    InsertData(MD);
                }
                if (MM.Rows.Count > 0)
                {
                    InsertData1(MM);
                }
            }

堆栈跟踪:-

    at System.Collections.Generic.List`1.set_Capacity(Int32 value)
    at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
    at System.Collections.Generic.List`1.Add(T item)
    at System.Data.DataView.ToTable(String tableName, Boolean distinct, String[] columnNames)

【问题讨论】:

  • 文件大小是多少?
  • 异常行(完整跟踪)。问题很可能是文件大小或文件中的条目数超过列表中允许的最大项目数。
  • 我正在尝试读取缺少 5 条记录的文本文件。
  • 不是您问题的答案,但您应该真正使用数组的Length 属性,而不是Count() 扩展方法。
  • 你有多少列?

标签: c#


【解决方案1】:

多次使用 String.Split 可能会导致 OutOfMemoryException。下载 Lumenworks Fast CSV Reader - 问题已解决。你会在这里得到它:http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

有关拆分问题的更多详细信息,请查看以下链接:

http://vpnchoudhary.blogspot.ie/2011/03/out-of-memory-exception-simple.html

string.split() "Out of memory exception" when reading tab separated file

相关引用在这里:

当您对包含 1355049 逗号的字符串进行拆分时会发生什么 分隔字符串,每个字符串包含 16 个字符,总字符长度 25745930 个?

指向字符串对象的指针数组:连续的虚拟地址 4(地址指针)*1355049 = 5420196(数组大小)+ 16(对于 簿记)= 5420212。非连续的虚拟地址空间 1355049 个字符串,每个 54 个字节。这并不意味着所有这些 1.3 百万个字符串将分散在整个堆中,但它们会 不在 LOH 上分配。 GC 会将它们分配到 Gen0 上的束上 堆。 Split.Function 将创建 System.Int32[] 的内部数组 大小为 25745930,消耗(102983736 字节)~98MB 的 LOH,非常 昂贵的L。

【讨论】:

  • "多次使用 String.Split 可能会导致 OutOfMemoryException。" -- 如果你要提出这样的声明,你真的应该引用你的消息来源。
  • 我提供了修改 - 请删除您的反对票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2013-01-14
  • 2013-08-28
  • 2015-07-18
相关资源
最近更新 更多