.Net Windows 服务内存不足异常

本文关键字:异常 内存不足 服务 Windows Net | 更新日期: 2023-09-27 18:30:18

请告诉我如何解决以下错误。以下内存不足异常仅在代码部署在生产计算机中(即 Windows 服务在生产计算机上启动并运行时)出现。它不会在我们的本地复制。

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()

我们在Windows服务中运行了一个计时器。

请在下面找到代码

        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)
            {
            }
        }
    }
}

提前感谢您的帮助。

.Net Windows 服务内存不足异常

计时器太多:

   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)
    {
    }
}

并在 OnStart 调用添加计时器中。Ps:尽可能避免继续。大多数时候是这样。