C# 通用列表 foreach OutofMemoryException

标签 c# foreach out-of-memory generic-list sharpkml

我有一个程序可以从数据库中读取大约 200 万行到列表中。 每行是一个包含地理坐标等信息的位置。

将数据添加到列表后,我使用 foreach 循环并获取坐标以创建一个 kml 文件。当行数很大时,循环会遇到 OutOfMemoryException 错误(但在其他情况下可以完美运行)。

关于如何处理这个问题以便程序可以处理非常大的数据集,有什么建议吗? kml 库是 SharpKML。

我还是 C# 的新手,所以请放轻松!

这是循环:

            using (SqlConnection conn = new SqlConnection(connstring))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(select, conn);

            using (cmd)
            {
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    double lat = reader.GetDouble(1);
                    double lon = reader.GetDouble(2);
                    string country = reader.GetString(3);
                    string county = reader.GetString(4);
                    double TIV = reader.GetDouble(5);
                    double cnpshare = reader.GetDouble(6);
                    double locshare = reader.GetDouble(7);

                    //Add results to list
                    results.Add(new data(lat, lon, country, county, TIV, cnpshare, locshare));
                }
                reader.Close();
            }
            conn.Close();
        }

            int count = results.Count();
            Console.WriteLine("number of rows in results = " + count.ToString());

            //This code segment generates the kml point plot

            Document doc = new Document();
            try
            {
                foreach (data l in results)
                {
                    Point point = new Point();
                    point.Coordinate = new Vector(l.lat, l.lon);

                    Placemark placemark = new Placemark();
                    placemark.Geometry = point;
                    placemark.Name = Convert.ToString(l.tiv);

                    doc.AddFeature(placemark);

                }
            }
            catch(OutOfMemoryException e)
            {
                throw e;
            }

这是列表中使用的类

        public class data
    {
        public double lat { get; set; }
        public double lon { get; set; }
        public string country { get; set; }
        public string county { get; set; }
        public double tiv { get; set; }
        public double cnpshare { get; set; }
        public double locshare { get; set; }

        public data(double lat, double lon, string country, string county, double tiv, double cnpshare,
            double locshare)
        {
            this.lat = lat;
            this.lon = lon;
            this.country = country;
            this.county = county;
            this.tiv = tiv;
            this.cnpshare = cnpshare;
            this.locshare = locshare;
        }

    }

最佳答案

为什么在写入之前需要存储所有数据?与其将每一行添加到列表中,不如在读取每一行时对其进行处理,然后将其忘掉。

例如,尝试像这样将您的代码组合在一起:

Document doc = new Document();
while (reader.Read())
{
    // read from db
    double lat = reader.GetDouble(1);
    double lon = reader.GetDouble(2);
    string country = reader.GetString(3);
    string county = reader.GetString(4);
    double TIV = reader.GetDouble(5);
    double cnpshare = reader.GetDouble(6);
    double locshare = reader.GetDouble(7);

    var currentData = new data(lat, lon, country, county, TIV, cnpshare, locshare));

    // write to file
    Point point = new Point();
    point.Coordinate = new Vector(currentData.lat, currentData.lon);

    Placemark placemark = new Placemark();
    placemark.Geometry = point;
    placemark.Name = Convert.ToString(currentData.tiv);

    doc.AddFeature(placemark);
}

这只有在 Document 被合理地实现时才有效。

关于C# 通用列表 foreach OutofMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000880/

相关文章:

c# - .Net 列表 ForEach Item

c# - 从C#中的最后一个对象开始循环

grails - 内存不足错误仍然存​​在

Python:垃圾收集器是否在引发 MemoryError 之前运行?

c# - 如何使用反射确定属性类型?

c# - 推荐哪种方法 : aggregateRoot. Items.Add(...) or aggregateRoot.AddItem(...)

php - php 的 foreach 语句中的这个冒号是什么?

android - 在 Canvas 上绘图时出现内存不足问题

c# - 如何使用 LINQ 将具有两个字段的对象列表转换为具有其中一个字段的数组

c# - 表达式树问题 - 如何将实例方法转换为 Func