2017-04-19 44 views
0

我有一个Windows服务与准备topshelf.This项目发送请求Web服务和响应XML数据。之后,这个XML数据序列化列表对象和比这些对象从SQL更新信息。当启动服务约8 MB后,增加1.8 GB内存超出范围exception.Why?TopShelf windows服务内存泄漏,同时更新信息从sql

public class HostService 
{ 

    private readonly Timer _updaterThread; 
    private readonly object _lockObject; 
    private readonly Manager _manager; 
    public HostService() 
    { 
     _manager = new Manager(); 
     _lockObject = new object(); 
     var interval = Convert.ToInt32(ConfigurationManager.AppSettings["UpdateInterval"]); 
     _updaterThread = new Timer(interval) { AutoReset = true }; 
     _updaterThread.Elapsed += UpdateInfo; 
    } 

    public void Start() 
    { 
     try 
     { 
      LoadLogger(); 
      _updaterThread.Start(); 

     } 
     catch (Exception e) 
     { 
      FXEventLogger.Instance().AddLog(EventLogEntryType.Error, e); 
     } 
    } 

    private void UpdateInfo(object state, EventArgs ev) 
    { 
     lock (_lockObject) 
     { 
      _manager.UpdateFmdProductions(); 
     } 
    } 



    public void Stop() 
    { 
     try 
     { 

      _updaterThread.Stop(); 
      FXEventLogger.Instance().AddLog(EventLogEntryType.SuccessAudit, "Service stopped"); 
      FXEventLogger.Finalize(); 
     } 
     catch (Exception e) 
     { 
      FXEventLogger.Instance().AddLog(EventLogEntryType.Error, e); 
     } 
    } 
+0

请秀“UpdateFmdProductions ();” – KreminT

+0

UpdateFmdProductions有两个方法调用 –

回答

0

** UpdateFmdProductions()做两个过程首先** `公共类SmdStatusProvider {

public List<MakerInfo> GetMakersInfo() 
    { 
     SMDStatusServiceClient requester = null; 
     try 
     { 
      requester = new SMDStatusServiceClient(); 

      var response = requester.Request("//MachineStatus[@MachineType='MAKER']/ShiftStatus"); 

      return ParseMakersInfo(response); 
     } 
     catch (Exception e) 
     { 
      FXEventLogger.Instance().AddLog(EventLogEntryType.Error, e); 

      return null; 
     } 
     finally 
     { 
      requester.Close(); 
      GC.SuppressFinalize(this); 
     } 
    } 


    private List<MakerInfo> ParseMakersInfo(string shiftStatesXml) 
    { 
     using (var sr = new StringReader(shiftStatesXml)) 
     { 
      var serializer = new XmlSerializer(typeof(List<MakerInfo>), new XmlRootAttribute("result")); 

      var _makersInfo = (List<MakerInfo>) serializer.Deserialize(sr); 

      sr.Dispose(); 

      return _makersInfo; 
     } 
    } 
}` 

公共类FMDRepository:SqlServerClient {

public FMDRepository(string connectionString) 
    { 
     ConnectionString = connectionString; 
    } 

    public bool UpdateRealProductions(IEnumerable<MakerInfo> makersInfo) 
    { 
     if (makersInfo == null) return false; 

     OpenConnection(); 

     foreach (var makerInfo in makersInfo) 
     { 
      var success = ExecuteNonQuery(
        String.Format("UPDATE [Shooter] SET [RealProduction]={0}, [LastUpdate]='{1:yyyy-MM-dd HH:mm:ss}' WHERE [Machine]='{2}'", 
         Math.Round(makerInfo.RealProduction, 0), DateTime.Now, 
         makerInfo.Machine)); 
      if (success == -1) 
      { 
       CloseConnection(); 
       return false; 
      } 
     } 
     CloseConnection(); 
     return true; 
    } 
}