String C#中奇怪的内存不足异常,使用列表、字符串或ado.net?
我有一个很大的疑问。这个问题是我班上的内存不足。但这里似乎有些奇怪。我有一个dll中的类String C#中奇怪的内存不足异常,使用列表、字符串或ado.net?,string,list,memory-management,ado.net,system.reactive,String,List,Memory Management,Ado.net,System.reactive,我有一个很大的疑问。这个问题是我班上的内存不足。但这里似乎有些奇怪。我有一个dll中的类 public class MyClass : IDisposible { List<ClassA> a_classLists = new .....// new instance. List<ClassB> b_classLists = new .....// new instance. public string Method1(int IDVa
public class MyClass : IDisposible
{
List<ClassA> a_classLists = new .....// new instance.
List<ClassB> b_classLists = new .....// new instance.
public string Method1(int IDValue)
{
// do here some web service call and get some XML data from it.
// parse the xml.
// Iterate through a for loop and add each node value to a_classLists
// Usually contains 10 or 15 items
Method2(); // from here calling another method
FinalSaveToDB(); // finally save the data to DB
return "";
}
private void Method2()
{
// do here some web service call and get some XML data from it.
// Iterate through a forloop.
// parse the xml. [large xml data. ie, image in binary format]
// For each loop add image binary data and other xml to b_classLists
// Usually it contains 50 or 60 such large lists.
}
private void FinalSaveToDB()
{
// using sqlbulkcopy, i am saving the data in the 2 lists to 2 different
// tables in the DB.
// Tab lock is mentioned in sqlbulk class.
// Actually 2 sqlbulkcopy class for 2 lists.
// Only 1 sql connection opens, then do the sqlbulkcopy. [there is no dataset or datareader]
// sqlconnection closes. I am using "using" clause for sqlconnection, bulkcopy etc
// these all are working fine.
}
private void Dispose()
{
// here nulling everything
// proxy null
// all the lists null....
}
}
因此,这里抛出错误,表示内存不足,评估失败
所以我关闭了VS中选项的字符串求值复选框
再说一次,没有用
因此,我将字符串更改为StringBuilder,并尝试每次追加活动字符串
还是没用。我不明白这里面有什么问题
这是因为所有线程都是并行工作的,它们是否交换MyClass资源???为什么不释放对象
请在这件事上帮助我。你能给出异常的堆栈跟踪以及它发生在哪一行代码上吗?@Patashu谢谢。没有显示堆栈跟踪错误。它的显示方式类似于-在mscorlib.dll中发生了“System.OutOfMemoryException”类型的异常,但未在用户代码中处理。“内部异常”窗口“内存外异常”类似于-函数求值已禁用,因为以前的函数求值超时。必须继续执行才能重新启用函数求值。在将流程活动添加到-Logdata=WriteLogs(异常错误@::+processname);//Logdata是MyClass中的类级字符串变量。@Patashu,这是writelog方法。私有字符串WriteLogs(string newstring){return Logdata+Environment.NewLine+newstring;}也许我遗漏了一些明显的东西,但是为什么还没有人指出dispose方法中的“null”对象完全不起任何作用呢?您应该关闭打开的连接,处理实现IDisposable等的对象,顺便说一句,使用(MyClass p=new MyClass()){return Observable.Start(()=>p.Method1(emp.ID),Scheduler.ThreadPool);在线程有机会启动之前处理对象}
private IObservable<string> SendEmpDetails(Employee emp)
{
using (MyClass p = new MyClass())
{
return Observable.Start(() => p.Method1(emp.ID), Scheduler.ThreadPool);
}
// here I hope it will call the Dispose and release all objects in the class.
}
// This EmployeeLists contains 1000 employee objects
EmployeeLists.ToObservable().Select(x => SendEmpDetails(x).Select(y => new { emp = x, retval = y }))
.Merge(10)
.ObserveOn(Scheduler.CurrentThread)
.Subscribe(x =>
{
SendStatus(x.retval.Item1, x.retval);
});
logdata = Environment.Newline() + "This method has completed";