We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

DataTable - memory leak

cartti
cartti asked
on
Medium Priority
2,326 Views
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++)
                {
                          dt.ImportRow(source.Rows[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?
Comment
Watch Question

Commented:
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.

Author

Commented:
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.

Thanks...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Did you try DataTable.Copy?

Bob

Commented:
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.
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.