DataTable - memory leak

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
carttiAuthor 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.

Bob LearnedCommented:
Did you try DataTable.Copy?

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.