在 RX Framework 中使用类时,我们如何释放类中的资源?我在 C# 4.0 lib 项目中有一个类,其中包含 Web 服务代理、ado.net 对象等,我也实现了 IDisposable。这个类有一个运行运行方法,在这个方法中,我将数据保存到数据集中,最后当方法完成时,我将数据集保存到 DB。
在 Dispose 方法中,我将数据集设置为 null,将其他 Web 服务代理对象设置为 null。然而,当此类在反应式扩展方法中频繁使用时,它会引发内存异常。
public class MyClass : IDisposable
{
proxy object;
DataSet object; // This dataset has 2 tables with relation set each other
public string LongRunMethods(string code)
{
// iterrating a for loop...
// insert new row into the 1st table of dataset in each loop
// another loop
// insert new row into the 2nd table of dataset in each loop
// Bulk save the dataset to Database
// return string;
}
private void Dispose()
{
// nulling all the objects...[proxies, datasets, etc
}
}
并且这个类是通过创建1000次来使用的,如下所示。
IObservable<string> RunProcess(Employee emp)
{
using (MyClass p = new MyClass ())
{
return Observable.Start(() => p.LongRunMethods(emp.Code), scheduler.ThreadPool);
}
}
此 EmployeeDatas 是 1000 个员工对象的列表。
EmployeeDatas.ToObservable().Select(x => RunProcess(x).Select(y => new { edata = x, retval = y }))
.Merge(10)
.ObserveOn(Scheduler.CurrentThread)
.Subscribe(x =>
{
SendReportStatus(x.retval.Item1, x.retval);
});
一切正常。但是当处理第 300 个或以上的员工对象时,有时会在 MyClass 中抛出内存不足的异常。
我在 MyClass 中使用 dispose 方法,一旦完成,它将释放所有资源。然而为什么内存不足异常。
这里有什么问题。