与流程相关的免费资源而不会将其删除

时间:2014-07-31 14:29:43

标签: c# .net

我从数据库中导出大量数据(特别是图像)我试图处理这些对象,但似乎内存分配不断堆积。请在这里帮助我的代码

foreach (var v in EntityFrameworkContext.GetInstance.DbContext.ATasks.ToList())                         
{

    if (v.TaskImages.Count == 0)
        continue;

    var dir = string.Format("{0}\\{1}", txtDumpFolder.Text, v.Id);

    if (!Directory.Exists(dir))
        Directory.CreateDirectory(string.Format("{0}\\{1}", txtDumpFolder.Text, v.Id));

    lstLog.Invoke((MethodInvoker)delegate()
        {
            lstLog.Items.Add("---------------------------------------------------------------------");
            lstLog.Items.Add(string.Format("DIRECTORY: {0} Created.", dir));

            int visibleItems = lstLog.ClientSize.Height / lstLog.ItemHeight;
            lstLog.TopIndex = Math.Max(lstLog.Items.Count - visibleItems + 1, 0);
        });

    v.TaskImages.ToList().ForEach(a =>
    {
        using (Bitmap bmp = new Bitmap(new MemoryStream(a.Image)))
        {
            PropertyItem[] pi = bmp.PropertyItems;
            bmp.Save(string.Format("{0}\\{1}.jpg", dir, a.Id), ImageFormat.Jpeg);

            LogMessage(string.Format("IMAGE: {0} \n exported.", a.Id));
        }
    });
    GC.Collect();
};

}

0 个答案:

没有答案