此代码会在引发OutOfMemoryException
之前不断增加内存使用量。我试图强制GC,但它没有帮助,ClusterResultDBDataContext
是一个Linq to Sql类。
对我来说一切都很好,有人能发现一些问题吗?
static void getDistinct()
{
var db = new ClusterResultDBDataContext();
var result = new LinkedList<Tuple<int,int>>();
using(var sw = new StreamWriter("result.txt"))
{
foreach(var y in db.Results)
{
var cnarr = y.PaperResults
.OrderBy(x => x.DOI).Select(x => x.ClusterNumber).ToArray();
var re = cnarr.Aggregate((x, a) => (x * 10 + a) % 19930727);
Console.WriteLine(y.ID+" " +re);
sw.WriteLine(y.ID + ","+re);
GC.Collect();
//result.AddLast(new Tuple<int, int>(y.ID, re));
}
}
答案 0 :(得分:0)
我认为您可以在执行任务和查询后关闭与db的连接,也不应该在每次迭代中调用db.Results。
试试这个:
static void getDistinct()
{
using (ClusterResultDBDataContext db = new ClusterResultDBDataContext())
{
var result = new LinkedList<Tuple<int, int>>();
using (var sw = new StreamWriter("result.txt"))
{
var results = db.Results;
foreach (var y in results)
{
var cnarr = y.PaperResults
.OrderBy(x => x.DOI)
.Select(x => x.ClusterNumber).ToArray();
var re = cnarr.Aggregate((x, a) => (x * 10 + a) % 19930727);
Console.WriteLine(y.ID + " " + re);
sw.WriteLine(y.ID + "," + re);
}
}
}
}