Click here to Skip to main content
15,898,371 members
Home / Discussions / C#
   

C#

 
GeneralRe: Simple Problem Pin
tatchung14-Jul-05 16:03
tatchung14-Jul-05 16:03 
GeneralRe: Simple Problem Pin
Christian Graus14-Jul-05 16:15
protectorChristian Graus14-Jul-05 16:15 
GeneralRe: Simple Problem Pin
tatchung14-Jul-05 16:22
tatchung14-Jul-05 16:22 
QuestionWhat mistake in this Asynchronous webservice Pin
Vinkum13-Jul-05 21:33
Vinkum13-Jul-05 21:33 
GeneralSet Time Zone Information in Windows CE Pin
myanne13-Jul-05 21:29
myanne13-Jul-05 21:29 
Generalc# code Pin
manar113-Jul-05 21:28
manar113-Jul-05 21:28 
GeneralRe: c# code Pin
J4amieC13-Jul-05 22:56
J4amieC13-Jul-05 22:56 
QuestionSOAP client in Windows Service - memory leak?? Pin
teddy_bear13-Jul-05 21:06
teddy_bear13-Jul-05 21:06 
I am experiencing a very strange and annoying problem Confused | :confused: . It's gonna take a while to describe it, so be ready to read a long post...
I have a Windows service written in C# that works as a scheduler - it loads objects implementing certain interface from other assemblies, and executes some of their methods when the time comes to do that. Nothing too complex... Let's say this interface looks like this:
public interface IPlugin<br />
{<br />
  void Execute();<br />
}


There's a core class - say SvcManager - which has Start(), Stop(), Pause() and Resume() methods that are called from the service' OnStart, OnStop, OnPause and OnContinue methods correspondingly. SvcManager.Start() creates one monitoring thread that is responsible for starting and aborting worker threads according to the schedule.

Worker thread creates an instance of an object implementing IPlugin interface, runs its Execute() method, and exits. The execution could take a while (several hours), so monitoring thread might need to stop the worker thread by calling Abort() method. SvcManager.Stop() aborts the monitoring thread and all worker threads (I keep all the plugin objects loaded, and running threads in the ArrayList), and waits for them to finish. Pause() and Resume() just pauses/resumes all these threads. It doesn't matter what the plugin objects do - they are all loaded from separate assemblies, and supposedly could do anything. Well, everything works perfect, unless any of the plugins implements SOAP client. After each SOAP call memory usage is increasing significantly, making it neccesserily to restart the service every 2 days or so, otherwise it eats all the memory on the machine at the rate of 100-150 Mb/day. It doesn't matter if I create a new instance of soap client proxy (inherited from SoapHttpClientProtocol) for each call and then explicitly call its Dispose() method in the finally block, or if I create just a single instance of a proxy which is called multiple times in a loop - in both cases memory leaks. I used .NET Memory Profiler from SciTech, and found that after each SOAP call a new set of objects appears in memory and never goes away - these are instances of AsyncCallback, HttpAbortDelegate, GCHandle[], Overlapped, OverlappedAsyncCallback, byte arrays and some other internal classes used by WebClientProtocol implementation - all referring to each other, and somehow keepeng themselfs in memory. When the thread executing SOAP calls exits normally - all these objects stay in memory. I tried to call GC.Collect() - it did not help. And if after Collect() I call GC.WaitForPendingFinalizers() method - it locks my app for several minutes, and still does not release the memory! But - and here comes interesting part - if I terminate the thread by calling Abort() method - the GC immediately destroys all these objects, and from now on the service starts working properly - all new threads executing SOAP calls do not fill up the memory anymore, and the counters for existing instances of all these classes stay below 10 all the time (although before aborting the thread they increase by 1000s/min), and memory does not grow. The tricky part is that thread must be aborted after at least one SOAP call has been made, and before it exits normally. Well, that's a very weird and unclean workaround for the problem Frown | :( ..

Also, as you may notice, the implementation of SvcManager class makes it very easy to execute exactly the same process in a standalone Win32 app (not a service). Actually, it is even the same application - if there is certain parameter in a command line, it starts as a normal WinForms application. And if I set it up to call SvcManager.Start() on start, and SvcManager.Stop() on exit - it makes it to do exactly what the service does. And in the case of standalone app memory does not leak!!! No tricks needed with aborting threads - it just works perfectly fine from the beginning and not accumulating any of these internal objects after SOAP calls...

So I am absolutely lost now. I used to work with unmanaged code, and never had any memory leak problems in the last 6 (or more) years, just making sure to always call the destructor/release the resources in a finally block... Now, when managed environment is supposed to take care of everything, I ran into this issue which drives me crazy Cry | :((

Anyone from Microsoft, or just a guru who knows well the internals of .NET framework - please help! Even though without looking at the code it's really hard to figure out what's going on (believe me - it's not easy when you're staring at the code either!), maybe based on my description you can just give me some hints and lead me to a nicier workaround then the one with aborting the threads... And it's actually not a small app - with complex scheduling etc, so rewriting it from scratch is really not an option right now. I could not track down the reason for GC for not collecting these objects, I do not know what ThreadAbortException exactly does does to the threads so it magically makes GC to start working properly, and I do not see why it could happen only if app is running as a service. If somebody could shed light upon these questions, it might help a lot.

Thanks!
Generalcrystal report Pin
yitong13-Jul-05 20:34
yitong13-Jul-05 20:34 
GeneralRe: crystal report Pin
User 209307313-Jul-05 21:30
User 209307313-Jul-05 21:30 
GeneralConsole App Input Pin
Expert Coming13-Jul-05 20:15
Expert Coming13-Jul-05 20:15 
GeneralRe: Console App Input Pin
seee sharp13-Jul-05 22:34
seee sharp13-Jul-05 22:34 
GeneralWindows Service Pin
ksanju100013-Jul-05 20:15
ksanju100013-Jul-05 20:15 
GeneralRe: Windows Service Pin
Vasudevan Deepak Kumar14-Jul-05 5:16
Vasudevan Deepak Kumar14-Jul-05 5:16 
GeneralWindows Service in c# Pin
ksanju100013-Jul-05 20:14
ksanju100013-Jul-05 20:14 
GeneralRe: Windows Service in c# Pin
Dave Kreskowiak14-Jul-05 4:18
mveDave Kreskowiak14-Jul-05 4:18 
Generaltree view issues Pin
Anonymous13-Jul-05 17:31
Anonymous13-Jul-05 17:31 
GeneralRe: tree view issues Pin
Rob Graham13-Jul-05 17:40
Rob Graham13-Jul-05 17:40 
GeneralRe: tree view issues Pin
Anonymous13-Jul-05 17:57
Anonymous13-Jul-05 17:57 
GeneralRe: tree view issues Pin
mav.northwind13-Jul-05 22:46
mav.northwind13-Jul-05 22:46 
GeneralRe: tree view issues Pin
Anonymous14-Jul-05 6:00
Anonymous14-Jul-05 6:00 
GeneralCombo box bind to object ... need help Pin
kakarato13-Jul-05 16:37
kakarato13-Jul-05 16:37 
GeneralRe: Combo box bind to object ... need help Pin
Christian Graus13-Jul-05 16:57
protectorChristian Graus13-Jul-05 16:57 
GeneralRe: Combo box bind to object ... need help Pin
kakarato13-Jul-05 20:39
kakarato13-Jul-05 20:39 
GeneralRe: Combo box bind to object ... need help Pin
J4amieC13-Jul-05 21:56
J4amieC13-Jul-05 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.