简体   繁体   English

从文本文件读取数据时抛出“System.OutOfMemoryException”类型的异常

[英]Exception of type 'System.OutOfMemoryException' was thrown while reading data from text file

I m trying to read a text file data with "|"我正在尝试使用“|”读取文本文件数据separated, I m using below code.分开,我使用下面的代码。 I m able to read the data from text file after reading when trying to put datatable data into dataview I m getting Exception of type 'System.OutOfMemoryException' was thrown,当我尝试将数据表数据放入数据视图时,我能够在读取后从文本文件中读取数据我得到了“System.OutOfMemoryException”类型异常被抛出,

Can any one suggest me how to avoid this exception.任何人都可以建议我如何避免这种异常。

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);
                }
            }

StackTrace :-堆栈跟踪 :-

    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)

Multiple uses of String.Split are likely to cause an OutOfMemoryException.多次使用 String.Split 可能会导致 OutOfMemoryException。 Download Lumenworks Fast CSV Reader - problem solved.下载 Lumenworks Fast CSV 阅读器 - 问题已解决。 You will get it here: http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader你会在这里得到它: http : //www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

For more details on issues with Split, have a look at the following links:有关 Split 问题的更多详细信息,请查看以下链接:

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

string.split() "Out of memory exception" when reading tab separated file string.split() 读取制表符分隔文件时出现“内存不足异常”

Relevant quote here:相关报价在这里:

What happens when you do a split on a string containing 1355049 comma separated strings of 16 characters each, having total character length of 25745930 ?当您对包含 1355049 个逗号分隔字符串的字符串进行拆分时会发生什么,每个字符串包含 16 个字符,总字符长度为 25745930 ?

An Array of pointers to string object: Contiguous virtual address space of 4 (address pointer)*1355049 = 5420196 (arrays size) + 16 (for book keeping) = 5420212. Non-contiguous virtual address space for 1355049 strings, each of 54 bytes.指向字符串对象的指针数组:4(地址指针)*1355049 = 5420196(数组大小)+ 16(用于簿记)= 5420212的连续虚拟地址空间。 1355049个字符串的非连续虚拟地址空间,每个54字节. It does not mean all those 1.3 million strings would be scattered all across the heap, but they will not be allocated on LOH.这并不意味着所有这 130 万个字符串都将分散在整个堆中,但它们不会在 LOH 上分配。 GC will allocate them on bunches on Gen0 heap. GC 将在 Gen0 堆上的束上分配它们。 Split.Function will create internal array of System.Int32[] of size 25745930, consuming (102983736 bytes) ~98MB of LOH, which is very expensive L. Split.Function 将创建大小为 25745930 的 System.Int32[] 内部数组,消耗(102983736 字节)~98MB 的 LOH,这是非常昂贵的 L。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取大文件-System.OutOfMemoryException:引发了类型为&#39;System.OutOfMemoryException&#39;的异常。 在C#中 - Reading large file - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. in C# 从数据表中选择数据时引发了类型&#39;System.OutOfMemoryException&#39;的异常 - Exception of type 'System.OutOfMemoryException' was thrown while selecting data from datatable System.OutOfMemoryException:angularjs中抛出了类型&#39;System.OutOfMemoryException&#39;的异常 - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown in angularjs System.OutOfMemoryException:抛出了“System.OutOfMemoryException”类型的异常 - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown “使用xmlserializer时抛出了类型&#39;System.OutOfMemoryException&#39;的异常 - “Exception of type 'System.OutOfMemoryException' was thrown” while using xmlserializer 上传Blob时抛出了类型'System.OutOfMemoryException'的异常 - Exception of type 'System.OutOfMemoryException' was thrown while uploading Blob 抛出了类型&#39;System.OutOfMemoryException&#39;的异常 - Exception of type 'System.OutOfMemoryException' was thrown 类型为&#39;system.outofmemoryexception&#39;的异常被抛出 - exception of type 'system.outofmemoryexception' was thrown 使用linq插入批量数据时发生错误{引发了&#39;System.OutOfMemoryException&#39;类型的异常。} - Error occured while inserting bulk data by using linq {Exception of type 'System.OutOfMemoryException' was thrown.} 异常:抛出 System.OutOfMemoryException - Exception: System.OutOfMemoryException was thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM