1
Answer

Threads inside threads = Memory Leak

Steven

Steven

15y
4.7k
1

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.

Answers (1)