【发布时间】:2016-06-27 13:40:13
【问题描述】:
我的程序内存不足。我不明白为什么我在“处理”时设置了object = null。
foreach (DataRow theRow in thisDataSet.Tables["Collection"].Rows)
{
LS_BatchAID = Convert.ToString(theRow["BatchAID"]);
LS_Batch = Convert.ToString(theRow["Batch"]);
LS_ID = Convert.ToString(theRow["ID"]);
CL_Batch Batch = new CL_Batch(LS_BatchAID, LS_ID, LS_Batch);
Batch = null;
}
thisConnection.Close();
我收到此错误:在 mscorlib.dll 中发生 System.OutOfMemoryException' 在运行程序和观察taskmanager的同时,我可以看到内存消耗随着代码的迭代次数线性上升。
我应该如何制作一个不会增加内存消耗/转储的程序?
CL_Batch:
class CL_Batch
{
private string BatchAID;
private string ID;
private string Batch;
private string pathPDF;
private string pathCPR;
private string pathLog;
private string DateXMLGenerated;
private string[] IDType;
private string[,] IDTypes;
private string[] Files;
private DateTime Dates;
private byte[] pdfContent;
private string XMLContent;
private string[] RefNbr;
public CL_Batch(string IV_BatchAID, string IV_ID, string IV_Batch)
{
this.Dates = DateTime.Now;
this.DatoXMLGenerated = "" + Dato.Date.Year.ToString() + "-" + BuildNumber(true, 2, Dato.Date.Month.ToString()) + "-" + BuildNumber(true, 2, Dato.Date.Day.ToString()) + "";
this.BatchAID = IV_BatchAID;
this.ID = IV_ID;
this.Batch = IV_Batch;
this.pathPDF = @"C:\path\TempFiles\path\" + this.ID + ".Pdf";
this.pathCPR = @"C:\path\TempFiles\";
this.pathLog = @"C:\path\Log\" + this.Batch + ".txt";
setRefnbr();
// Set array with mappings of ID between partners.
setLegitimationsTyper();
// ensure log is available ( [NameOfLog] ).
prepareLog();
// Find all files for archive.
getFileNames();
// Move files C:\path\TempFiles\
if (this.getFiles() == true)
{
// Create PDF's.
makePDF();
// Insert PDF's in database.
insertPDF();
// Create XML files.
makeXML();
// Insertt XML in database.
insertXML();
}
public string getBatchAID()
{
return this.BatchAID;
}
public string getID()
{
return this.ID;
}
public string getBatch()
{
return this.Batch;
}
public string getIDTyper(string IV_Code, bool kode)
{
for (int i = 0; i <= this.IDTypes.GetUpperBound(0); i++)
{
if (this.IDTypes[i, 0] == IV_Kode)
{
if (Code == true)
{
return this.LegitimationsTyper[i, 1];
}
else
{
return this.LegitimationsTyper[i, 2];
}
}
}
return "";
}
}
/******************************************/
/** 更新 #1 **************************/
很公平!滥用构造函数。我明白了 - 但是: 真正的问题是什么?
如果我按照我已经做的那样做对比这个例子:
CL_Batch Batch = new CL_Batch(LS_BatchAID, LS_ID, LS_Batch);
Batch.setRefnbr();
Batch.setIDTypes();
Batch.prepareLog();
Batch.getFileNames();
Batch.makePDF();
Batch.insertPDF();
Batch.makeXML();
Batch.insertXML();
Batch = null;
那么真正的区别是什么? 如果是用不同的方法来添加几个数字,那么最终会得到相同的指令。
First program:
xor ax, ax
mov ax, 10
add ax, 10
Second program:
xor ax, ax
mov ax, 10
add ax, 10
我认为最终没有区别(我认为我误用了 oop 的概念,但最终产品是相同的 - 我希望)
请就我的错觉给我建议。
提前致谢。 /** 更新 #1 / /******************************************/
【问题讨论】:
-
什么是 CL_Batch?
-
将你的 sn-p 降低到
foreach并不是一种将噪音降到最低的方法 - 如果你只是降低整个班级的噪音并向我们展示这个减少的班级会更好 -你永远不知道它可能不在你的 foreach 循环中 -
CL_Batch的构造函数是什么?为什么你要把你刚刚创建的对象清空?这闻起来像是被滥用的构造函数。
-
我们仍然缺少相关代码。构造函数正在调用未包含的方法。问题可能是任何事情。但是你不应该在你的构造函数中有“做某事”的行为。很明显,您这样做是因为您正在创建对象然后将其设置为 null,这表明仅创建对象会导致某些事情发生。我们应该创建一个对象,然后对这个对象做一些事情。构造函数本身不应该是类的行为所在。
-
恕我直言,尝试学习面向对象编程的基础知识。构造函数应该用来构造一个对象,而不是执行各种代码。这不仅会使您创建的对象变得无用,还会使其无法用于其他用例。
标签: c# object out-of-memory