Lavanya
Lavanya

Reputation: 1

.Net Windows service out of Memory exception

Please let me know how to resolve the following error. Below Out of Memory exception only arises once the code is deployed in Production Machine (i.e once Windows service is up and running on production Machine). It is not replicating in our local.

Application: LSRAnalysisService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
Stack:
   at System.TimeZoneInfo.GetDaylightTime(Int32, AdjustmentRule)
   at System.TimeZoneInfo.GetIsDaylightSavingsFromUtc(System.DateTime, Int32, System.TimeSpan, AdjustmentRule, Boolean ByRef)
   at System.TimeZoneInfo.GetUtcOffsetFromUtc(System.DateTime, System.TimeZoneInfo, Boolean ByRef, Boolean ByRef)
   at System.DateTime.ToLocalTime(Boolean)
   at System.DateTime.FromFileTime(Int64)
   at System.Timers.ElapsedEventArgs..ctor(Int32, Int32)
   at System.Timers.Timer.MyTimerCallback(System.Object)
   at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.TimerQueueTimer.CallCallback()
   at System.Threading.TimerQueueTimer.Fire()
   at System.Threading.TimerQueue.FireQueuedTimerCompletion(System.Object)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

We have a Timer running in windows service.

Please find the code below

        protected override void OnStart(string[] args)
        {
            if (wcfserviceHost != null)
                wcfserviceHost.Close();
            wcfserviceHost = new ServiceHost(typeof(AnalysisManager));
            wcfserviceHost.Open();
            Deletefile();
        }

        protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Deletefile();
        }

        private void Deletefile()
        {
            try
            {
                timer = new System.Timers.Timer();
                timer.Interval = (1000) * 60 * 60;//(10000) * 6;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                string FilePath = System.Configuration.ConfigurationSettings.AppSettings["FilePath"];
                DirectoryInfo dirInfo = new DirectoryInfo(FilePath);
                foreach (DirectoryInfo subDirectory in dirInfo.GetDirectories())
                {
                    if (subDirectory.Name == "Temp") continue;
                    if (subDirectory.CreationTime < DateTime.Now.AddDays(-1))
                    {
                    subDirectory.Delete(true);
                    }
                }
                timer.Enabled = true;
                timer.Start();
            }
            catch(Exception ex)
            {

            }

        }
    }
}

Thanks in Advance for your help.

Upvotes: 0

Views: 2480

Answers (1)

Peter Krassoi
Peter Krassoi

Reputation: 567

Too many timers:

   private void addtimer()
{
    timer = new System.Timers.Timer();
    timer.Interval = (1000) * 60 * 60;//(10000) * 6;
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    timer.Enabled = true;
    timer.Start();

}
protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    Deletefile();
}
private void Deletefile()
{
    try
    {
        string FilePath = System.Configuration.ConfigurationSettings.AppSettings["FilePath"];
        DirectoryInfo dirInfo = new DirectoryInfo(FilePath);
        foreach (DirectoryInfo subDirectory in dirInfo.GetDirectories())
        {
            if (subDirectory.Name != "Temp" && subDirectory.CreationTime < DateTime.Now.AddDays(-1))
                subDirectory.Delete(true);
        }
    }
    catch (Exception ex)
    {

    }

}

and in OnStart call addTimer. Ps: avoid continue whenever it's possible. And most time it is.

Upvotes: 2

Related Questions