DataTable - memory leak

Posted on 2006-04-03
Last Modified: 2008-01-09
I have code that copies the structure and contents of a DataTable into another DataTable object. The problem I'm having is, everytime the code is run, memory for the application according to Task Manager creeps up by a meg. The code I'm using is:

dt_temp = GetCopiedDataTable(m_tblStatic);

private DataTable GetCopiedDataTable(DataTable source)
        DataTable dt = source.Clone();
                for (int i = 0; i<source.Rows.Count;i++)
        return dt;

I was under the impression that if dt_temp is reassigned, the garbage collection would release resources that used for the previous reference. However, it doesn't release any memory at all. Any ideas?
Question by:cartti
    LVL 4

    Expert Comment

    1.) Is dt_temp part of a DataSet? If so, then the DataSet still has a reference to the table, and the GC can't free it.
    2.) Is dt_temp referenced anywhere else? If so, those other references are keeping it alive.

    Also, using TaskManager as a guage for memory consumption is folly. You should be using the performance counters in PerfMon.

    Also, the CLR will continue to increase its working set and not lower it (even though the memory is getting freed) until the system is in a low memory state, or Windows specifically tells the CLR to back off.

    If you have 1GB of ram and using 128MB of it, why should the CLR spend a bunch of time rearranging the working set size (an expensive operation) when it doesn't have to?

    If you're really concerned about it, you can call the Win32 API function SetWorkingSetSize, but I would do some research on this before doing it, because you CAN do some harm to your process because now you're essentially fighting with the GC and the CLR memory subsystem.
    LVL 2

    Author Comment

    Thanks c_myers for your in depth response...

    dt_temp is a DataTable. It is assigned to the DataSource property of a DataGrid object. That is its only reference. If the routine is called again, I would have thought the memory would be freed up if the DataSource was reassigned, but it never is - it just grows. I will check out alternative memory consumption utilities in the meantime.

    LVL 96

    Expert Comment

    by:Bob Learned
    Did you try DataTable.Copy?

    LVL 4

    Expert Comment

    How do you create the datatable?

    Is it:

    DataTable dt_temp = new DataTable()?

    If that's the case, and then you assign it as the data source to a grid (is this ASP.NET or WinForms?), and then, later, re-assign it to something else, you're right, the original dt_temp ref should be flagged for garbage collection.
    LVL 20

    Accepted Solution

    It's normal that the memory increases. You have the old table and create a new one. The new one takes new memory. The old one can be released during the next garbage collection session if you free all references to it but the next garbage collection session will not come soon if your machine has enough memory. So you don't need to bother about that, just release the objects you don't need (release all handles to them).

    Additionally consider the DataTable.Copy() method as Bob suggested. It is surely faster.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now