使用大型阵列时程序崩溃 [英] Program crashes when working with large array
问题描述
我的程序循环遍历一个空元素数组,将每个元素分配给一个新对象(我制作的类的实例).数组变得非常大-大约有60,000个元素.有时,此循环会起作用,但通常程序会崩溃,并出现通用的Microsoft错误"x.exe遇到问题,需要关闭...".我已经得出结论,这不是内存 size 问题,但是可能是某种内存管理问题...
这是Point类:
My program loops through an array of null elements, assigning each element to a new object (an instance of a class I made). The array gets quite large - about 60,000 elements in size. On occasion, this loop will work, but generally the program will crash, giving a generic microsoft error "x.exe has encountered a problem and needs to close...". I have concluded that this is not a memory size issue, but could be a memory management issue of some sort...
Here is the Point class:
public partial class Point : System.Windows.Window
{
private string m_ID; // Theoretically to be used only for the transmitter point locations... Could be used for other points too depending on application...
public double x, y, power;
public string m_time;
public Point()
{
power = 0;
}
public Point(double x, double y)
{
this.x = x; this.y = y; power = 0;
}
public Point(double x, double y, int column)
{
this.x = x; this.y = y; power = 0;
}
public Point(double x, double y, int column, string time)
{
m_time = time; this.x = x; this.y = y; power = 0;
}
public Point(double x, double y, int column, string time, double power)
{
m_time = time; this.x = x; this.y = y; this.power = power;
}
/// <summary>
/// THE LABEL OR NAME OF THE GIVEN POINT.
/// </summary>
public string ID
{
get
{
return m_ID;
}
set
{
m_ID = value;
}
}
public string time
{
get
{
return m_time;
}
set
{
m_time = value;
}
}
}
这是构建Point's数组的函数:
And here is the function which builds the array of Point''s:
public Point[] getPoints(ExcelInterface excelData)
{
int longColumn = excelData.FindColumnNumber("Longitude");
int latColumn = excelData.FindColumnNumber("Latitude");
int timeColumn = excelData.FindColumnNumber("Time");
int powerColumn = excelData.FindColumnNumber("Power_dBm");
Point[] dataPoints = new Point[excelData.rows];
string hh, mm, ss, MM, dd;
hh = string.Empty; mm = string.Empty; ss = string.Empty; MM = string.Empty; dd = string.Empty;
int hour, minute, second;
double power;
DateTime time;
double[] thresholds = getPowerThresholds(numThresholds, excelData);
string dateTime, timeString;
if (System.DateTime.Now.Month < 10)
MM = "0" + System.DateTime.Now.Month.ToString();
else MM = System.DateTime.Now.Month.ToString();
if (System.DateTime.Now.Day < 10)
dd = "0" + System.DateTime.Now.Day.ToString ();
else dd = System.DateTime.Now.Day.ToString();
string date = System.DateTime.Now.Year + "-" + MM + "-" + dd;
double lat, lon;
// array stop between 1900 and 2000 in size...
for (int i = 2; i < excelData.rows; i++)
{
if (i % 400 == 0)
{
}
time = new DateTime(); // DEBUG
lat = (double)excelData.GetCellValue(i, latColumn, 1);
lon = (double)excelData.GetCellValue(i, longColumn, 1);
time = (DateTime)excelData.GetCellValue(i, timeColumn, 1);
power = (double)excelData.GetCellValue(i, powerColumn, 1);
hour = time.Hour;
if (hour < 10)
hh = "0" + hour.ToString();
else hh = hour.ToString();
minute = time.Minute;
if (minute < 10)
mm = "0" + minute.ToString();
else mm = minute.ToString();
second = time.Second;
if (second < 10)
ss = "0" + second.ToString();
else ss = second.ToString();
timeString = "T" + hh + ":" + mm + ":" + ss + "Z";
dateTime = date + timeString;
try
{
dataPoints[i - 2] = new Point(lon, lat, i, dateTime);
}
catch (Exception ex)
{
}
}
我还通过仅使用构造函数简化了错误发生的循环,从而进行了测试:
I have also tested this by simplifying the loop where the error occurrs, by using only the constructor:
for (int i = 2; i &lt; excelData.rows; i++)
{
try
{
dataPoints[i - 2] = new Point(0, 0, 0, "");
}
catch (Exception ex)
{
}
}
在此测试方案下,程序仍然崩溃.我还构建了一个简单的测试程序,该程序仅创建一个点数组并将每个数组元素分配给一个新的Point.该程序可以很好地处理超过一百万个元素...
我真的很感谢您的帮助-我已经为此努力了几天!
Rich
The program still crashed with this test scenario. I have also built a simple test program which solely creates a point array and assigns each array element to a new Point. That program worked just fine with over 1million elements...
I would really appreciate any help - Ive been wrestling with this for days!
Rich
推荐答案
Erm ...您是否有任何理由要从 System.Windows.Window 继承,因为我怀疑这是您当前的原因问题?
您可以像在轻量级结构中一样轻松地存储点信息.您还可以消除日期内容周围的许多字符串操作...
我通常不喜欢仅仅给出答案,但是您的示例中有很多多余的内容,这能胜任吗?
Erm...is there any reason your inheriting from System.Windows.Window as I suspect this is cause of your current issue?
You could store your point information just as easily in a lightweight struct. You could also eliminate alot of the string operations around the date stuff...
I don''t usually like just giving out answers, but there is a lot of excess compexity in your example, will this do the job?
public struct Point
{
public double Longitude{get;set;}
public double Latitude{get;set;}
public double Power{get;set;}
public DateTime Time{get;set;}
}
public Point[] getPoints(ExcelInterface excelData)
{
int longColumn = excelData.FindColumnNumber("Longitude");
int latColumn = excelData.FindColumnNumber("Latitude");
int timeColumn = excelData.FindColumnNumber("Time");
int powerColumn = excelData.FindColumnNumber("Power_dBm");
Point[] dataPoints = new Point[excelData.rows];
DateTime date = DateTime.Now.Date()
double[] thresholds = getPowerThresholds(numThresholds, excelData); // Don't know what this is for
double lat, lon, power;
// array stop between 1900 and 2000 in size...
List<Point> points = new List<Point>();
for (int i = 2; i < excelData.rows; i++)
{
Point p = new Point
{
Longitude = (double)excelData.GetCellValue(i, longColumn, 1)
,Latitude = (double)excelData.GetCellValue(i, latColumn, 1)
,Time = date.Add(((DateTime)excelData.GetCellValue(i, timeColumn, 1)).TimeOfDay)
, Power = (double)excelData.GetCellValue(i, powerColumn, 1)
}
points.Add(p)
}
return points.ToArray();
}
哦,对不起,我在这篇文章之前删除了Windows继承.不太确定为什么要这么做...但这不是问题的原因-删除继承并没有阻止错误.实际上,使用列表而不是数组可以解决它!
现在已经解决了这个问题,我遇到了另一个奇怪的问题……我有一个格式化某些点节点并将这些节点放入kml的函数.这些点由Google Earth显示.在foreach循环中循环时,出现通用的Windows错误消息,并且程序在任意行崩溃.实际上,当我调试和逐步执行代码时,在步骤之间出现错误...
这是代码:
Oh, sorry, the windows inheritance I removed prior to this post. Not too sure why I did that... That was not the cause of the issue though - removing the inheritance didn''t stop the error. Actually, using a list instead of an array solved it!
Now that this one is solved, I have run into another strange issue... I have a function that formats some point nodes and places these nodes into a kml. These points are displayed by google earth. While looping through the foreach loop, a generic windows error message appears and the program crashes at a random line. Actually, while I was debugging and stepping through the code, an error occurred in between steps...
Heres is the code:
private void plotPoints(Point[] points, double[] thresholds)
{
string node;
string style = "";
System.Xml.XPath.XPathNavigator navigator = xDoc.CreateNavigator();
navigator.MoveToChild("kml", "http://www.opengis.net/kml/2.2");
navigator.MoveToFirstChild();
if (isNewKml())
insertFolder("Points", navigator);
else // delete folder
{
deleteFolder("Points");
insertFolder("Points", navigator);
}
navigator = getNavigator(navigator, "Points");
foreach (Point p in points)
{
if (p != null)
{
if (1 % 1000 == 0)
{ }
// Errors occur here at random places...
// Check power thresholding and apply colors accordingly
for (int n = 0; n < thresholds.Length; n++) // Error here
{// error here
if (p.power < thresholds[n])
{
style = "CustomStyle" + (n + 1).ToString();
break;
}
else if (n == (thresholds.Length - 1)) // Gets the case where a power measurement is above the average max (20 highest measurements)
style = "CustomStyle" + (n + 1).ToString();
}
object[] arguments = { pointIdex, System.Environment.NewLine, p.time, System.Environment.NewLine, style, System.Environment.NewLine, p.x, p.y };
// Error here (above line) while waiting (not stepping)!
node = String.Format("<name>Point{0}</name>{1}<TimeStamp> <when>{2}</when> </TimeStamp>{3}<styleUrl>#{4}</styleUrl>{5}<Point> <coordinates>{6},{7}, 0</coordinates> </Point>");
navigator.MoveToFirstChild();
navigator.InsertElementAfter(navigator.Prefix, "Placemark", navigator.LookupNamespace(navigator.Prefix), null); // "Point" + pointIdex
navigator.MoveToNext();
// navigator.MoveToFirstChild(); // not used?
navigator.AppendChild(node);
pointIdex++;
navigator.MoveToParent();
}
}
}
}
有谁知道为什么会这样?
Does anyone have any idea why this might be?
这篇关于使用大型阵列时程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!