|
-
August 23rd, 2009, 10:13 AM
#1
free memory allocated by datatable
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
-
August 23rd, 2009, 08:56 PM
#2
Re: free memory allocated by datatable
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
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
August 24th, 2009, 03:12 AM
#3
Re: free memory allocated by datatable
The using statement maybe used to 'force' the garbage collector to step in and tidy memory immediately.
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.
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|