2011-02-23 29 views
0

我有一個Windows服務。該服務從Active目錄獲取用戶數據,然後在本地系統上保存一個xml文件。該服務使用一個計時器,在每個(可以說)10分鐘後工作。當它第一次運行時,它會消耗大約85 MB的內存,10分鐘後消耗118 MB,等等。每次運行時,都會消耗額外的內存。Windows服務,內存增加,每次它從活動目錄獲取數據

有人可以幫助我理解,我在這裏想念什麼。

protected override void OnStart(string[] args) 
{ 
    StartProcess(); 
} 

private void StartProcess() 
{ 
    sEvent = "AD XML Generator Started at " + System.DateTime.Now.ToString(); 

    if (!EventLog.SourceExists(sSource)) 
     EventLog.CreateEventSource(sSource, sLog); 

    EventLog.WriteEntry(sSource, sEvent); 


    ActiveDirectoryGenericFunctions obj = new ActiveDirectoryGenericFunctions(); 
    obj.GenerateXMLFileForAllUsers(DomainA, DomainB, xmlFileName); 
    RunService(); 
    //this.Stop(); 
} 
protected override void OnStop() 
{ 
    //Write to Event Log - Stop of Service also written to Evenbt automatically 

    sEvent = "AD XML Generator Stopped at " + System.DateTime.Now.ToString(); 

    if (!EventLog.SourceExists(sSource)) 
     EventLog.CreateEventSource(sSource, sLog); 

    EventLog.WriteEntry(sSource, sEvent); 


} 

private void RunService() 
{ 
    _timer = new Timer(); 
    _timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    _timer.AutoReset = true; // AutoReset = true means after specified interval the timer'll automatically be restarted 
    _timer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["ServiceHour"].ToString());// 300000; //5minutes// (0.1*60*60*1000); // specify some interval here in milliseconds e.g. 4 hours (4*60*60*1000) 
    _timer.Start();      
} 

void timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    RunService(); 
    StartProcess(); 
} 
+0

初始化定時器一次是什麼GenerateXMLFileForAllUsers辦? – 2011-02-23 22:18:19

+0

您每次觸發Elapsed處理程序時都會創建Timer對象。在OnStart中只創建一次定時器。 – 2011-02-23 22:25:22

回答

2

貌似每次通話時間RunService()您創建一個新的Timer但不處理舊之一。在你的代碼

編輯

尋找多一點的你實際上是創建一個新Timer相當頻繁。您的服務以OnStart()開頭,該號碼爲StartProcess(),該號碼爲RunService()。當該方法中的計時器觸發時,您同時撥打RunService()StartProcess(),後者再次撥打RunService()

這是我敢肯定的代碼應該看看,我徹底擺脫了RunService()方法和StartProcess()

protected override void OnStart(string[] args) 
{ 
    StartProcess(); 
} 

private void StartProcess() 
{ 
    sEvent = "AD XML Generator Started at " + System.DateTime.Now.ToString(); 

    if (!EventLog.SourceExists(sSource)) 
     EventLog.CreateEventSource(sSource, sLog); 

    EventLog.WriteEntry(sSource, sEvent); 


    ActiveDirectoryGenericFunctions obj = new ActiveDirectoryGenericFunctions(); 
    obj.GenerateXMLFileForAllUsers(DomainA, DomainB, xmlFileName); 

    //Create the timer once 
    if(_timer != null){ 
     _timer = new Timer(); 
     _timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
     _timer.AutoReset = true; // AutoReset = true means after specified interval the timer'll automatically be restarted 
     _timer.Interval = Convert.ToDouble(ConfigurationManager.AppSettings["ServiceHour"].ToString());// 300000; //5minutes// (0.1*60*60*1000); // specify some interval here in milliseconds e.g. 4 hours (4*60*60*1000) 
     _timer.Start(); 
    } 
} 
protected override void OnStop() 
{ 
    //Dispose the timer 
    _timer.Dispose(); 
    //Write to Event Log - Stop of Service also written to Evenbt automatically 

    sEvent = "AD XML Generator Stopped at " + System.DateTime.Now.ToString(); 

    if (!EventLog.SourceExists(sSource)) 
     EventLog.CreateEventSource(sSource, sLog); 

    EventLog.WriteEntry(sSource, sEvent); 
} 


void timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    StartProcess(); 
} 
+0

我該如何處理使用過的線程? – Nimish 2011-02-24 02:33:45

+0

如果StartProcess尚未完成,那麼定時器已經過去了,它會導致錯誤? – Reynan 2017-04-27 05:36:33