C#程序内存不足
C# program Out of memory
我有一个程序内存不足。我不明白为什么我设置 object = null
时 "handled".
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();
我收到此错误:System.OutOfMemoryException' 发生在 mscorlib.dll
在 运行 程序和观察任务管理器时,我可以看到内存消耗随着代码的迭代次数线性上升。
我应该如何制作一个不增加内存的程序consumption/dump?
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 /
/*********************************************/
这是暗中操作,因为我们看不到您的代码。您正在创建 PDF。这通常涉及某种 COM 对象或内存流。也许您用来创建这些 PDF 的任何内容都没有被处理或清理,因此您创建的每个 PDF 都在内存中,直到您 运行 退出。我会仔细查看您正在使用的任何组件的文档。如果某些东西实现了 IDisposable
,请确保你正在处理它。
尽管我不同意关于错误的构造函数代码的提议,但我必须承认它产生了影响,并且在从构造函数中删除代码后代码按预期工作。
如果有人对我有很好的解释,那么我想听听。
无论如何我可以给你们(BugFinder,
卡勒姆·林宁顿,
混合剑突,
马诺德斯特拉
迈克·罗宾逊,
马修怀特,
Scott Hannen)的解决方案功劳?
这是有效的代码:
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.setRefnbr();
Batch.setIDTypes();
Batch.prepareLog();
Batch.getFileNames();
Batch.makePDF();
Batch.insertPDF();
Batch.makeXML();
Batch.insertXML();
}
thisConnection.Close();
我有一个程序内存不足。我不明白为什么我设置 object = null
时 "handled".
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();
我收到此错误:System.OutOfMemoryException' 发生在 mscorlib.dll 在 运行 程序和观察任务管理器时,我可以看到内存消耗随着代码的迭代次数线性上升。
我应该如何制作一个不增加内存的程序consumption/dump?
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 / /*********************************************/
这是暗中操作,因为我们看不到您的代码。您正在创建 PDF。这通常涉及某种 COM 对象或内存流。也许您用来创建这些 PDF 的任何内容都没有被处理或清理,因此您创建的每个 PDF 都在内存中,直到您 运行 退出。我会仔细查看您正在使用的任何组件的文档。如果某些东西实现了 IDisposable
,请确保你正在处理它。
尽管我不同意关于错误的构造函数代码的提议,但我必须承认它产生了影响,并且在从构造函数中删除代码后代码按预期工作。 如果有人对我有很好的解释,那么我想听听。 无论如何我可以给你们(BugFinder, 卡勒姆·林宁顿, 混合剑突, 马诺德斯特拉 迈克·罗宾逊, 马修怀特, Scott Hannen)的解决方案功劳?
这是有效的代码:
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.setRefnbr();
Batch.setIDTypes();
Batch.prepareLog();
Batch.getFileNames();
Batch.makePDF();
Batch.insertPDF();
Batch.makeXML();
Batch.insertXML();
}
thisConnection.Close();