gpt4 book ai didi

使用 StreamWriter 写入文本文件时出现 C# OutOfMemoryException

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:25 25 4
gpt4 key购买 nike

我查看了与此问题类似的答案,但我认为我仍然不明白为什么会出现异常。

根据堆栈跟踪,我正在尝试调试在写入文本文件时抛出“OutOfMemoryException”的一段代码。从以下行抛出异常:

WriteToLogFile("Found in EnumerateActiveDirectoryFilteredMembers: " + e.Message);

WriteToLogFile方法如下:

    static void WriteToLogFile(string strLine)
{
using (StreamWriter sw = new StreamWriter(strLogFileName, true))
{
sw.WriteLine("[" + DateTime.Now.ToString() + "]: " + strLine);
}
}

看起来 StreamWriter 对象有时会抛出这个异常,但不是每次调用该方法时都会在该方法的末尾重新分配内存吗?并且使用“using”关键字不是确保该对象被丢弃吗?

当抛出异常时,该文件只有 13 KB - 那么这里到底发生了什么?

更新:OutOfMemoryException 不是 WriteToLogFile 方法。它被抛出得更早,但我不知道这是从哪里来的……我将在 catch 之前添加一个 try 语句的框架:

        try
{
if (objSearchResults.Count != 0)
{
// ADD TO DATASET HERE //
string tableName = "tempADhold";
DataSet domains = new DataSet();
domains.Tables.Add(tableName);

//add x number of columns
domains.Tables[tableName].Columns.Add("DomainID", typeof(string));
.
.
.

// Get the object from AD.
foreach (SearchResult objResultUser in objSearchResults)
{
// Init variables and init formatting of strings for adding objects
string DomainID = ""; string UserName = ""; string Title = "";
.
.
.

objUserEntry = objResultUser.GetDirectoryEntry();
// Populate obj and LDAP path variables.
// Perform edits before setting variables.
// Match strings to SQL Server table sizes.

// add objects in same manner:
// DomainID
if (objUserEntry.Properties["samAccountName"].Count > 0)
{
DomainID = objUserEntry.Properties["samAccountName"].Value.ToString();
if (DomainID.Substring(0, 1).ToUpper() != "N")
{
WriteToLogFile("RECORD SKIPPED: Invalid N_Number: " + DomainID);
continue; //skip this record
}
else if (DomainID.Length > 8)
{
WriteToLogFile("RECORD SKIPPED: Invalid N_Number length. #s ending in 'c' should be skipped: " + DomainID);
continue; //skip this record
}

}
.
.
.

//set up array struct for adding obj

ADUserList[0] = DomainID; ADUserList[1] = UserName;
ADUserList[2] = ...; ADUserList[3] = ...;
ADUserList[4] = ...; ADUserList[5] = ...;
ADUserList[6] = ...; ADUserList[7] = ...;
ADUserList[8] = ...; ADUserList[9] = ...;
ADUserList[10] = ...; ADUserList[11] = ...;
ADUserList[12] = ...; ADUserList[13] = ...;
ADUserList[14] = ...; ADUserList[15] = ...;
ADUserList[16] = ...;

if (ADUsersIndex > 0 && ADUsersIndex % 2500 == 0)
{
//Add to dataset instead of array here...
if (InsertRows(domains.Tables[tableName]).Equals(false)) { return false; }
domains.Tables[tableName].Rows.Clear();
}

DataRow myRow = domains.Tables[tableName].NewRow();
myRow.ItemArray = ADUserList;
domains.Tables[tableName].Rows.Add(myRow);
ADUsersIndex++;

}
//Write the last rows to the database.
if (InsertRows(domains.Tables[tableName]).Equals(false)) { return false; }
objSearchResults.Dispose();


} //end "if (objSearchResults.Count != 0)"

else
{
WriteToLogFile("Results: No Active Directory filtered members found.");
}
}
catch (Exception e)
{
if (e is OutOfMemoryException) throw;
WriteToConsole("Error in EnumerateActiveDirectoryFilteredMembers: " + e.Message);
return true;
}

最佳答案

正在尝试运行时抛出此消息

WriteToLogFile("Found in EnumerateActiveDirectoryFilteredMembers: " + e.Message);

在错误处理程序中

e 几乎可以肯定已经是一个 OutOfMemory 异常,但是您的处理程序在尝试记录消息时抛出,“隐藏”了真正的堆栈跟踪。

在 WriteToLogFile 行之前将其添加到您的错误处理程序中:

if (e is OutOfMemoryException) throw;

这仍然会导致应用程序停止运行,但会为您提供原始错误的真实 StackTrace。

关于使用 StreamWriter 写入文本文件时出现 C# OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25209484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com