For example, if you load a xml and use it for whatever reason:
XDocument doc = XDocument.Load("my.xml");
Now that you have finished using it and want to release any memory used by it, initally i tought that setting it t开发者_StackOverflow中文版o null would do the job but it does not, so what is the proper way to release the used memory if that is possible ?
You are doing the proper thing. Let the GC worry about when it collects as it will when there's pressure.
The proper way is to simple stop using doc
. The garbage collector will clean up the memory the next time it's invoked and determines doc
is no longer referenced by your code.
If doc
is a local the JIT will determine the last point it's used and make it eligable for collection at that point. There is no need to null
it out. Raymond Chen has an excellent blog article explaining how this works
- http://blogs.msdn.com/b/oldnewthing/archive/2010/08/10/10048149.aspx
精彩评论