c# - Memory leak happens when using thread inside another thread -


below sample code has memory leak. if comment out 2 lines inside refreshtimer_elapsed, memory leak gone. know what's wrong? help.

    static void refreshtimer_elapsed(object sender, system.timers.elapsedeventargs e)     {         thread innerthread = new thread(delegate() { });         innerthread.start();     }      static void main(string[] args)     {         system.timers.timer refreshtimer = new system.timers.timer();         refreshtimer.interval = 5000;         refreshtimer.elapsed += new system.timers.elapsedeventhandler(refreshtimer_elapsed);         refreshtimer.start();          (; ; )         { }                } 

are sure theres memory leak? or notice memory grows?

until garbage collector cleans threads create, memory grow, not leaking, garbage collector knows dead memory.

the way memory "leaks" in managed enviroment .net or java when have objects being referenced never used or needed. thats not case here. you're creating bunch of threads , forget them immediately. they're no longer referenced refreshtimer_elapsed , thread stops running there no more references , free cleaned.

you won't see memory drop until garbage collector ready cleanup. can try , force not recommended performance reasons.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -