Saturday, 15 September 2012

c# - Memory leak using timers -



c# - Memory leak using timers -

i have piece of code ants profiler points causing memory leak. have monitored application on period of 1 week, memory seems increasing , not coming back. i'm bit concerned below code.

public void printxml(xmldocument doc) { //system.threading.timer timer = null; xmlwritersettings settings = new xmlwritersettings { encoding = encoding.utf8, indent = true }; new system.threading.timer((_) => { using (var author = xmlwriter.create(_folderdestination, settings)) { // task.delay(15000).continuewith(_ => doc.save(writer)); doc.save(writer); } }).change(15000, -1); }

everytime method printxml called write doc _folderdestination after period of 15secs. want achieve. above code seems leaking memory , memory never returns back. if help optimize it, great.

system.threading.timer implements idisposable.

wrap within using statement create sure gets disposed properly.

if purpose of timer delay execution, alternative way can utilize new thread , thread.sleep.

public void printxml(xmldocument doc) { var thread = new system.threading.thread(new system.threading.parameterizedthreadstart(delayprint)); thread.start(doc); } void delayprint(object param) { system.threading.thread.sleep(15000); xmldocument doc = param xmldocument; // work }

c# .net timer

No comments:

Post a Comment