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

  • Home
  • SEARCH
  • 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 8801869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:56:38+00:00 2026-06-14T00:56:38+00:00

Yet another How-to-free-memory question: I’m copying data between two databases which are currently identical

  • 0

Yet another How-to-free-memory question:

I’m copying data between two databases which are currently identical but will soon be getting out of sync. I have put together a skeleton app in C# using Reflection and ADO.Net Entities that does this:

For each table in the source database:

  • Clear the corresponding table in the destination database
  • For each object in the source table
    • For each property in the source object
      • If an identically-named property exists in the destination object, use Reflection to copy the source property to the destination property

This works great until I get to the big 900MB table that has user-uploaded files in it.

The process of copying the blobs (up to 7 MB each) to my machine and back to the destination database uses up local memory. However, that memory isn’t getting freed, and the process dies once it’s copied about 750 MB worth of data – with my program having 1500 MB of allocated space when the OutOfMemoryException is thrown, presumably two copies of everything that it’s copied so far.

I tried a naive approach first, doing a simple copy. It worked on every table until I got to the big one. I have tried forcing a GC.Collect() with no obvious change to the results. I’ve also tried putting the actual copy into a separate function in hopes that the reference going out of scope would help it get GCed. I even put a Thread.Sleep in to try to give background processes more time to run. All of these have had no effect.

Here’s the relevant code as it exists right now:

public static void CopyFrom<TSource, TDest>(this ObjectSet<TDest> Dest, ObjectSet<TSource> Source, bool SaveChanges, ObjectContext context)
    where TSource : class
    where TDest : class {

    int total = Source.Count();
    int count = 0;
    foreach (var src in Source) {
        count++;
        CopyObject(src, Dest);

        if (SaveChanges && context != null) {
            context.SaveChanges();
            GC.Collect();
            if (count % 100 == 0) {
                Thread.Sleep(2000);
            }
        }
    }
}

I didn’t include the CopyObject() function, it just uses reflection to evaluate the properties of src and put them into identically-named properties in a new object to be appended to Dest.

SaveChanges is a Boolean variable passed in saying that this extra processing should be done, it’s only true on the big table, false otherwise.

So, my question: How can I modify this code to not run me out of memory?

  • 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-14T00:56:39+00:00Added an answer on June 14, 2026 at 12:56 am

    The problem is that your database context utilizes a lot of caching internally, and it’s holding onto a lot of your information and preventing the garbage collector from freeing it (whether you call Collect or not).

    This means that your context is defined at too high of a scope. (It appears, based on your edit, that you’re using it across tables. That’s…not good.) You haven’t shown where it is defined, but wherever it is it should probably be at a lower level. Keep in mind that because of connection pooling creating new contexts is not expensive, and based on your use cases you shouldn’t need to rely on a lot of the cached info (because you’re not touching items more than once) so frequently creating new contexts shouldn’t add performance costs, even though it’s substantially decreasing your memory footprint.

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

Sidebar

Related Questions

Yet another PHP question. I've got two arrays: one string-based and the other numeric.
Yet another IE is doing something different from other browsers question but this is
I have yet another question regarding the randomization of data! $gender=''; if(isset($_GET['gender'])) { $gender=$_GET['gender'];
So Yet another SMS question :-) I researched, but could not find a solution
Yet another question on this I know, but let me just say I looked
Yet another simple question about unit testing model objects that uses data access layer.
Yet another data structure doubt. I will get straight to it. This is what
I have yet another issue which the answer is eluding me. I wish to
I'm trying to produce yet another lightbox as much needed HTML/CSS/Javascript practice, but I've
Yet another question, go me!... Anyway, I have 2 classes with private constructors and

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.