Hi alli,
in one method I fill a datatable.
The application allocates 10MB of memory and even if the datatable is declared inside a local method, I am not able to free the memory (I used dispose ...)
Many thanks
Printable View
Hi alli,
in one method I fill a datatable.
The application allocates 10MB of memory and even if the datatable is declared inside a local method, I am not able to free the memory (I used dispose ...)
Many thanks
In C# memory management is performed by the garbage collector. Usually, it is a good idea to let it do it's stuff, but in some scenarios the developer requires the memory to be returned immediately. The garbage collector will only free up objects which are no longer referenced. As you state your Dataset is a local variable and therefore at the end of the function, references will be dropped and at sometime in the future the garbage collector will tidy it up.
FYI: Even though the Dataset is a local variable, memory for it will still be allocated on the heap, as opposed to the stack.
The using statement maybe used to 'force' the garbage collector to step in and tidy memory immediately. For example:
Code:using (DataSet ds = new DataSet())
{
ds.ReadXml(@"C:\test.xml");
// do some stuff
} // ds memory returned to available heap
That's not quite right. The using statement is just a way to release *certain kinds* of resources immediately without having to wait for the GC. Examples include sockets and file handles. If you forget to close these yourself, the .NET garbage collector will eventually run and will eventually close the handles for you, but this provides a way for you to deterministically release those (limited) resources.Quote:
The using statement maybe used to 'force' the garbage collector to step in and tidy memory immediately.
Calling Dispose (and using the 'using' pattern), does not make the GC do anything. It's completely separate to the GC. As for the original question, the memory is 'released' as soon as you don't have any live references to it anymore. Whenever the GC runs next it'll be collected. Your job is done.