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 9134431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:39:29+00:00 2026-06-17T08:39:29+00:00

I am experiencing a bad memory leak which occurs in the following code: public

  • 0

I am experiencing a bad memory leak which occurs in the following code:

 public void BulkInsert(string tableName, IDataReader reader, String connectionString)
    {
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (var bulkCopy = new SqlBulkCopy(connection))
            {
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.BulkCopyTimeout = 900;

                try
                {
                    bulkCopy.WriteToServer(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    reader.Close();
                }
            }
        }
    }

This code segment is executed thousands of times and as such it only takes a minute for an out of memory exception to occur. Ants reports that this is because the rows pointed to by the IDataReader are not being collected by the GC. However when I comment out the following line there is no leak which is how i’ve isolated the issue to this peice of code.

bulkCopy.WriteToServer(reader);

Has anyone a suggestion as to how to prevent the memory leak?

Thank you in advance.

Calling Code:

 var reader = datatable.CreateDataReader();
 BulkInsert(tablename, reader, connectionString);
 reader.Dispose();
 datatable.Dispose();
  • 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-06-17T08:39:30+00:00Added an answer on June 17, 2026 at 8:39 am

    I absolutely hate answering my own question but I finally found the solution and although I feel dumb for making this mistake but I just wanted to post it as the answer in case anyone else comes across this question and so nobody else wastes valuable time on this.

    The Memory Leak was not with the provided code segment at all – In fact it was not a leak at all…

    Looking at the Performance Analysis I discovered that speedwise the BulkCopy call was the bottleneck of my whole program. I have a Producer-Consumer pattern feeding it DataTables for it to insert.

    I thought the Memory Profiler was showing me DataTable objects which were not being disposed of when I ran the code. These were in-fact queued tables waiting to be inserted but because I was re-using test data the queued tables were already in the DB (and as such appeared to have already been inserted).

    By commenting out the BulkCopy line I was in effect removing the bottleneck, the DataTables were being quickly disposed, and thus no issues showed on the Memory Profiler. This made it appear like the Bulkcopy line was at fault.

    When I replaced the BulkCopy code with a 1s delay this was not enough of a bottleneck. It was only when I replaced the BulkCopy with a 5s delay that I noticed my Consumer Producer Queue Size getting out of control…. Hence why I was quickly running out of memory.

    Thanks to all who took time on this question. I’m sorry it did not have a more interesting answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im experiencing a strange situation which i was supposed to handle. The situation is:
I'm currently experiencing some problems with my server. I have a pool which is
I am experiencing a very weird problem: In WPF I have a tabControl which
I'm using linq2sql and .net 3.5 with vs2010. I'm experiencing bad sql performance when
I'm experiencing a bad looking graphical glitch with UIDatePicker, and I'm wondering if anyone
I have a customer reporting a supposed resource leak in our product which used
On a website, I'm experiencing a flash of white that occurs between page loads.
I've been experiencing the good and the bad sides of messaging systems in real
Experiencing jquery in rails through .js.erb templates; but completely puzzled by a simple problem
When experiencing networking problems on client machines, I'd like to be able to run

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.