469,945 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Cleaning up managed and unmanaged resources.

Hi experts,

I am writing a financial application which is going to run for days without restart. I understand the use of IDisposible to clean up all resources and preventing memory leak. But in my case I can't afford to allow GC to run for cleaning up all non-references resources. So, is there any way I can clean up all managed and unmanaged resources after using them to prevent GC. For example assigning null value to reference will still keep the non-referenced memory reserved until GC cleans it up and adds it to free managed memory. But I want to keep the GC out of pictures because it will block all running threads for time being. Any suggestion or solution will be a great help!!!

Thanks,
Oct 13 '09 #1
3 4547
tlhintoq
3,525 Expert 2GB
But in my case I can't afford to allow GC to run for cleaning up all non-references resources.
A) Why do you think that?
B) What do you think you can do to stop it? GC happens. Period. You can cause it to happen *now* when you know you need to. But I don't think you can stop it.
Oct 13 '09 #2
PRR
750 Expert 512MB
GC will run and you can do nothing about it. Its automatic and non deterministic, meaning you cannot determine/ guess when garbage collection will run.
Expand|Select|Wrap|Line Numbers
  1. object o = "123";
  2. object k = o;
  3.             o = null;
  4. //
  5.  
1.Assigning null reference to managed objects wont help. GC automatically releases the memory for objects that are no longer referenced. In above eg, o references string "123" and then k references o. If o is assigned null, it wont change anything wrt to memory that has string "123" (it wont just disappear). Object k still references string "123". Only when no objects reference string "123", GC will run and clear memory referenced by string "123".

2. For unmanaged objects its necessary to implement IDisposable.
"Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object."
MSDN

Link

"The garbage collector's optimizing engine determines the best time to perform a collection based on the allocations being made. When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots. Every application has a set of roots. Each root either refers to an object on the managed heap or is set to null. An application's roots include global and static object pointers, local variables and reference object parameters on a thread's stack, and CPU registers. The garbage collector has access to the list of active roots that the just-in-time (JIT) compiler and the runtime maintain. Using this list, it examines an application's roots, and in the process creates a graph that contains all the objects that are reachable from the roots."
MSDN
Oct 13 '09 #3
Thanks guys for the quick reply. I knew that it is not possible the way I want but still wanted to get expert's suggestion. Thanks Again...
Oct 13 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Anthony leong kar keong | last post: by
2 posts views Thread by SpotNet | last post: by
25 posts views Thread by Koliber (js) | last post: by
1 post views Thread by =?Utf-8?B?THVib21pcg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.