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.
There are a few blatent possibilities here.
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.
You are not using close or flush functions of the library
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.
You must close SMTPClient object connection to prevent memory leaks.
SmtpClient client = new SmtpClient("SMTPServerAddress"); ... client.Send(message); client.ServicePoint.CloseConnectionGroup(client.ServicePoint.ConnectionName);
精彩评论