1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Thread inside thread = Memory leak

Discussion in 'C#' started by semorri, Apr 22, 2009.

  1. semorri

    semorri New Member

    Joined:
    Apr 22, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone explain why this causes a memory leak? static void Main() { Thread outerThread = new Thread(delegate() { while (true) { Thread innerThread = new Thread(delegate() { }); innerThread.Start(); Thread.Sleep(10); } }); outerThread.Start(); Application.Run(); }By the way, if instead of creating an inner thread I just create an object, the garbage collector does its job and no memory leak occurs.
     
  2. ljlong

    ljlong New Member

    Joined:
    Dec 30, 2008
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software engineer, embedded, rtos, machine control
    Location:
    Santa Rosa, CA
    I think this is just like in java the garbage collector will not clean up orphan threads.
     
  3. semorri

    semorri New Member

    Joined:
    Apr 22, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    The same example in Java does not leak.
     

Share This Page