المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : C# 2.0 Windows Service - Memory Leak?



C# Programming
06-01-2010, 08:20 AM
I have a windows service in C# .net 2.0. At the moment all it is doing is being instantiated, setting up a timer and then every interval (eg 1 minute) writing to a log file. The memory usage on TaskManager goes up and up ...

I can't see any objects that I've left lying around - I've taken out pretty much everything it is meant to do ... perhaps I'm being paranoid and the garbage collector will come and clean up when it is ready? How long should I leave it? It's been a while since the last Windows Service I wrote ... any ideas?

Thanks in advance http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

Here are some excerpts from the code...

The LogFile is a static class with a single static method "Write"

public static class LogFile
{

public static void Write(string message)
{
string logFileName = (String)(new AppSettingsReader()).GetValue("log-file-path", typeof(String)) + "KBRFileLoaderServiceLog.txt";

File.AppendAllText(logFileName, DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss") + " KBRFileLoaderService:" + " " + message + Environment.NewLine);
}

The Service class itself
public partial class FileLoaderService : ServiceBase
{

private Timer serviceTimer;

public FileLoaderService()
{
InitializeComponent();

serviceTimer = new Timer();
double intervalInMinutes = (double)(new AppSettingsReader()).GetValue("timer-interval-in-minutes", typeof(double));
serviceTimer.Interval = intervalInMinutes * 60 * 1000; //intervalInMinutes * 60 seconds * 1000ms
serviceTimer.Elapsed += new ElapsedEventHandler(serviceTimer_Elapsed);
}

protected override void OnStart(string[] args)
{
LogFile.Write("Service Started - starting timer");
serviceTimer.Start();
}

protected void serviceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//stop timer whilst we process
serviceTimer.Stop();

//do processing
//have removed this to try to find the "leak"
LogFile.Write("Checking Queue...");

//processing done - start it up again
serviceTimer.Start();
}

protected override void OnStop()
{
serviceTimer.Stop();
serviceTimer = null;
LogFile.Write("Service Stopped");
}
}

And Program Main ...

static class Program
{
/// /// The main entry point for the application.
/// static void Main()
{
ServiceBase[] ServicesToRun;

//// More than one user Service may run within the same process. To add
//// another service to this process, change the following line to
//// create a second service object. For example,
////
//// ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
////
ServicesToRun = new ServiceBase[] { new FileLoaderService() };

ServiceBase.Run(ServicesToRun);

}
}