简体   繁体   中英

.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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM