Reputation: 1
I have a windows Service. This service gets user data from Active directory and then saves a xml file on the local system. The service uses a timer, to work after every (lets say) 10 minutes. When it runs for the first time, it consumes some 85 MB of memory, after 10 minutes, it consumes 118 MB, and so on. Everytime it runs, it consumes extra memory.
Can someone help me in understanding, what am I missing here.
protected override void OnStart(string[] args)
{
StartProcess();
}
private void StartProcess()
{
sEvent = "AD XML Generator Started at " + System.DateTime.Now.ToString();
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
ActiveDirectoryGenericFunctions obj = new ActiveDirectoryGenericFunctions();
obj.GenerateXMLFileForAllUsers(DomainA, DomainB, xmlFileName);
RunService();
//this.Stop();
}
protected override void OnStop()
{
//Write to Event Log - Stop of Service also written to Evenbt automatically
sEvent = "AD XML Generator Stopped at " + System.DateTime.Now.ToString();
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
}
private void RunService()
{
_timer = new Timer();
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
_timer.AutoReset = true; // AutoReset = true means after specified interval the timer'll automatically be restarted
_timer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["ServiceHour"].ToString());// 300000; //5minutes// (0.1*60*60*1000); // specify some interval here in milliseconds e.g. 4 hours (4*60*60*1000)
_timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
RunService();
StartProcess();
}
Upvotes: 0
Views: 2440
Reputation: 55457
Looks like every time you call RunService()
you create a new Timer
but don't dispose of the old one.
EDIT
Looking at your code a little more you're actually creating a new Timer
fairly often. Your service starts with OnStart()
which calls StartProcess()
which calls RunService()
. When the timer in that method fires you call both RunService()
and StartProcess()
, the latter which calls RunService()
again.
This is how I'm pretty sure the code should really look, I completely got rid of the RunService()
method and initialized the timer once in StartProcess()
protected override void OnStart(string[] args)
{
StartProcess();
}
private void StartProcess()
{
sEvent = "AD XML Generator Started at " + System.DateTime.Now.ToString();
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
ActiveDirectoryGenericFunctions obj = new ActiveDirectoryGenericFunctions();
obj.GenerateXMLFileForAllUsers(DomainA, DomainB, xmlFileName);
//Create the timer once
if(_timer != null){
_timer = new Timer();
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
_timer.AutoReset = true; // AutoReset = true means after specified interval the timer'll automatically be restarted
_timer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["ServiceHour"].ToString());// 300000; //5minutes// (0.1*60*60*1000); // specify some interval here in milliseconds e.g. 4 hours (4*60*60*1000)
_timer.Start();
}
}
protected override void OnStop()
{
//Dispose the timer
_timer.Dispose();
//Write to Event Log - Stop of Service also written to Evenbt automatically
sEvent = "AD XML Generator Stopped at " + System.DateTime.Now.ToString();
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);
EventLog.WriteEntry(sSource, sEvent);
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
StartProcess();
}
Upvotes: 2