c# - Windows Service, Memory increases everytime it gets data from Active Directory -


i have windows service. service gets user data active directory , saves xml file on local system. service uses timer, work after every (lets say) 10 minutes. when runs first time, consumes 85 mb of memory, after 10 minutes, consumes 118 mb, , on. everytime runs, consumes memory.

can me in understanding, missing here.

protected override void onstart(string[] args) {     startprocess(); }  private void startprocess() {     sevent = "ad xml generator started @ " + 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 event log - stop of service written evenbt automatically      sevent = "ad xml generator stopped @ " + 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 timer'll automatically restarted      _timer.interval = convert.todouble(configurationmanager.appsettings["servicehour"].tostring());// 300000; //5minutes// (0.1*60*60*1000); // specify interval here in milliseconds e.g. 4 hours (4*60*60*1000)     _timer.start();                        }   void timer_elapsed(object sender, elapsedeventargs e) {     runservice();     startprocess(); } 

looks every time call runservice() create new timer don't dispose of old one.

edit

looking @ code little more you're creating new timer often. service starts onstart() calls startprocess() calls runservice(). when timer in method fires call both runservice() , startprocess(), latter calls runservice() again.

this how i'm pretty sure code should look, got rid of runservice() method , initialized timer once in startprocess()

protected override void onstart(string[] args) {     startprocess(); }  private void startprocess() {     sevent = "ad xml generator started @ " + 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 timer once     if(_timer != null){         _timer = new timer();         _timer.elapsed  += new elapsedeventhandler(timer_elapsed);         _timer.autoreset = true; // autoreset = true means after specified interval timer'll automatically restarted          _timer.interval = convert.todouble(configurationmanager.appsettings["servicehour"].tostring());// 300000; //5minutes// (0.1*60*60*1000); // specify interval here in milliseconds e.g. 4 hours (4*60*60*1000)         _timer.start();        } } protected override void onstop() {     //dispose timer     _timer.dispose();     //write event log - stop of service written evenbt automatically      sevent = "ad xml generator stopped @ " + system.datetime.now.tostring();      if (!eventlog.sourceexists(ssource))         eventlog.createeventsource(ssource, slog);      eventlog.writeentry(ssource, sevent); }    void timer_elapsed(object sender, elapsedeventargs e) {     startprocess(); } 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -