Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 455823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:24:35+00:00 2026-05-12T22:24:35+00:00

I am trying to isolate the source of a memory leak in my C#

  • 0

I am trying to isolate the source of a “memory leak” in my C# application. This application copies a large number of potentially large files into records in a database using the image column type in SQL Server. I am using a LinqToSql and associated objects for all database access.

The main loop iterates over a list of files and inserts. After removing much boilerplate and error handling, it looks like this:

foreach (Document doc in ImportDocs) {
    using (var dc = new DocumentClassesDataContext(connection)) {
        byte[] contents = File.ReadAllBytes(doc.FileName);

        DocumentSubmission submission = new DocumentSubmission() {
            Content = contents,
            // other fields
        };

        dc.DocumentSubmissions.InsertOnSubmit(submission);  // (A)
        dc.SubmitChanges();                                 // (B)
    }
}

Running this program over the entire input results in an eventual OutOfMemoryException. CLR Profiler reveals that 99% of the heap consists of large byte[] objects corresponding to the sizes of the files.

If I comment both lines A and B, this leak goes away. If I uncomment only line A, the leak comes back. I don’t understand how this is possible, as dc is disposed for every iteration of the loop.

Has anyone encountered this before? I suspect directly calling stored procedures or doing inserts will avoid this leak, but I’d like to understand this before trying something else. What is going on?

Update

Including GC.Collect(); after line (B) appears to make no significant change to any case. This does not surprise me much, as CLR Profiler was showing a good number of GC events without explicitly inducing them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:24:36+00:00Added an answer on May 12, 2026 at 10:24 pm

    I don’t entirely understand why, but making a copy of the iterating variable fixed it. As near as I can tell, LinqToSql was somehow making a copy of the DocumentSubmission inside each Document.

    foreach (Document doc in ImportDocs) {
        // make copy of doc that lives inside loop scope
        Document copydoc = new Document() {
            field1 = doc.field1,
            field2 = doc.field2,
            // complete copy
        };
    
        using (var dc = new DocumentClassesDataContext(connection)) {
            byte[] contents = File.ReadAllBytes(copydoc.FileName);
    
            DocumentSubmission submission = new DocumentSubmission() {
                Content = contents,
                // other fields
            };
    
            dc.DocumentSubmissions.InsertOnSubmit(submission);  // (A)
            dc.SubmitChanges();                                 // (B)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse C++/Java style source files and would like to isolate
I'm trying to isolate duplicates in a 500MB database and have tried two ways
I've been trying to isolate a bug in my application. I succeeded in producing
I'm trying to isolate the percentage value in a string of text. This should
I have simplified an issue that I've been having trying to isolate the problem,
I am trying to isolate my menu inflation to only two items. I have
I'm trying isolate a problem I'm having making an XML RPC call: XDocument doc
i'm trying to isolate all the server names (i.e sv012-cali) from within an html
XML trickiness. I'm trying to isolate the node with a sports Match that is
All - Need some insight into this issue. I have created a sample project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.