views:

43

answers:

2

Hello everybody!

I have a windows service, which is configured to send emails in a predefined schedule. The schedule works with System.Timers.Timer and every time the Timer_Elapsed is raised I call Timer.Stop(), send emails (each time about 1500 emails), calculate the amount of time the next tick will be raised and start the timer (calling Timer.Start() method). The problem is when the timer is elapsed and the process started sending emails, the memory in use increases but not decreases after finishing. When I call the function in a "not timered" application, the used memory is freed up after finishing sending process. Can anybody help me to understand why this is going on? Maybe there's something concerning to threads used in timer?

Thanks in advance.

+1  A: 

There are a few blatent possibilities here.

  1. Garbage collection has not kicked in and there is no memory leak. Monitor over time to see how dynamic the memory usage is and see if it peaks and settles.

  2. You are not using close or flush functions of the library

  3. Your timer is starting a thread which never terminates. This is easy to see using a process monitor and watching the thread counts.

Memory leaks, while not impossible by any means, are improbable in .net languages. You are not directly accessing or controlling memory. Since the JIT does the memory allocation and clearing all you really need to check for is things not being freed in code.

Without specifics I cant give better help, especially not knowing if you are doing file IO etc.

Jacob
I am almost sure that the fragment of code which send the emails have not any unused references or unclosed connections after completing. I have placed MailMessage in using{} scope so the only disposable and unmanaged resources are freed up I think.What concerning to threads count, I am not sure and inspecting now.
Garegin
I second the threading idea, perhaps you are creating a new environment inadvertantly due to timer's ontick() or what have you. Also, garbage collection is weird. be sure to measure memory usage over more than two iterations of sleep/email/sleep, it could be 'caching' the email utilities.
Karl
I thought such a way too, that it may be caching some resources for email and and reuses. I have measured many iterations of sleep/email/sleep and it was very surprising when the memory increases in every iteration.Jacob, i have counted threads count, it increases and after finishing decreases but not to the same count. It was 11, during the process increases to 17 and decreases to 14. Something strange is going on...
Garegin
A: 

You must close SMTPClient object connection to prevent memory leaks.

SmtpClient client = new SmtpClient("SMTPServerAddress"); ... client.Send(message); client.ServicePoint.CloseConnectionGroup(client.ServicePoint.ConnectionName);

Karen