Erre Efe
Erre Efe

Reputation: 15557

Reduce memory footprint of File operations

I'm trying to run this method, it works fine but every time after some hundreds of internal iterations I get with an Out of Memory exception:

...
MNDBEntities db = new MNDBEntities();
var regs = new List<DOCUMENTS>();
var query = from reg in db.DOCUMENTS
            where reg.TAG_KEYS.Any(p => p.TAG_DATE_VALUES.FirstOrDefault().TAG_DATE_VALUE.HasValue 
                && p.TAG_DATE_VALUES.FirstOrDefault().TAG_DATE_VALUE.Value.Year == 2012)
            select reg;

var pages = new List<string>();
foreach (var item in query)
{
    Document cert = new Document();

    var tags = item.TAG_KEYS;
    foreach (var tag in tags)
    {
        // Basic stuff...
    }

    var pagesS = item.PAGES;
    foreach (var page in pagesS)
    {
        var path = @"C:\Kumquat\" + (int)page.NUMBER + ".vpimg";
        File.WriteAllBytes(path, page.IMAGE);
        pages.Add(path);
        Console.WriteLine(path);
    }

    //cms.Save(cert, pages.ToArray()).Wait();
    foreach (var pageFile in pages)
        File.Delete(pageFile);

    pagesS = null;
    pages.Clear();
}
...

I'm pretty sure problem is related with the File.WriteAllBytes or the File.Delete because if I comment those lines the method runs without exception. What I'm doing is basically get some tags from a DB plus a document image, that image is then saved onto disk then a stored into a cms and then deleted from disk. Honestly don't figure out what I'm doing wrong with that File calls. Any idea?

This is what PerfView shows:

PerfView of the method

This is what visual studio 2012 profiler shows as the hot point, the thing is: this is all generated code (within the Entity Model) am I doing something wrong maybe with the properties of the model?

enter image description here

Upvotes: 2

Views: 995

Answers (1)

Feng Yuan
Feng Yuan

Reputation: 707

Try to use http://www.microsoft.com/en-us/download/details.aspx?id=28567 to profile your code, focusing on GC events, and CLR managed allocation tick events.

page.IMAGE could be the problem. Most likely it will allocate a byte array and never delete it. Best to change the code to:

page.WriteTo(path);

The rest of the code shown does look fine. The only possible problem is large object allocation, which could lead to fragmentation problem in LOH.

Upvotes: 2

Related Questions