473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 4717
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

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

Similar topics

5
by: Anthony leong kar keong | last post by:
Hi i would like to ensure some code cleaning methods. #1 COM objects Is the following the correct way ? objMe = null; #2 Dispose VS. null Mydataset.dispose(); or
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
2
by: SpotNet | last post by:
Hi Newsgroup, General C# questions. If I have a class(es), of API functions just as they are. Would that be considered Unmanaged or unsafe (not meant as the c# keyword) code? If I implement...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
3
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
3
by: DaTurk | last post by:
Hi, I'm implementing the Idisposable pattern and am wondering what the difference it between managed, and unmanaged resources? http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx in the...
25
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class...
1
by: =?Utf-8?B?THVib21pcg==?= | last post by:
Hi, The unmanaged resources should be cleaned up by the source code directly, like for example database connection. I am wondering how to distinguish between managed and unmanaged resources,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.