Mono: Memory Leak with new threads

Created on 25 Jan 2018  路  5Comments  路  Source: mono/mono

Steps to Reproduce

  1. Create some new Threads with new Thread( new ThreadStart( ThreadFunction ).Start()
  2. Do garbage collection
  3. The thread class instances are not freed
  4. See sample code.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace MemTest {
  static class Program {

    [STAThread]
    static void Main() {
      new Thread( new ThreadStart( ThreadGC ) ).Start();

      while( true ) {
        Test();
        Thread.Sleep( 1 );
      }
    }

    static List<WeakReference> mList = new List<WeakReference>();

    static void Test() {
      var t = new Thread( new ThreadStart( ThreadTest ) );

      lock( mList ) {
        mList.Add( new WeakReference( t ) );
      }
      t.Start();
    }

    static void ThreadTest() {
      Thread.Sleep( 1 );
    }

    static void Count() {
      lock( mList ) {
        var tmp = mList.Where( c => c.IsAlive ).ToList();
        mList = tmp;
        Console.Write( "\n" + mList.Count + "\n" );
// the List grows and grows
      }
    }

    static void DoGC() {
      GC.Collect();
      Count();
    }

    static void ThreadGC() {
      while( true ) {
        if( !running ) break;
        Thread.Sleep( 1000 );
        DoGC();
      }
    }
  }
}

Current Behavior

Memory Leak

Expected [Behavior]

In sample code, the list of Threads in WeakReference should not grow

On which platforms did you notice this

[ ] macOS
[ X] Linux
[ ] Windows

Version Used: 5.11.0.15

Stacktrace

Please paste the stacktrace here if available.

Most helpful comment

will try on monday with lastest nightly 5.11.0.300 from https://download.mono-project.com/sources/mono/nightly/

All 5 comments

I cannot reproduce with Mono 5.11.0.320 (master/693a7c896d9). Could you try downloading latest package from https://jenkins.mono-project.com/view/All/job/build-package-osx-mono/job/master/ and try again? Thank you

will try on monday with lastest nightly 5.11.0.300 from https://download.mono-project.com/sources/mono/nightly/

yes. no memory leak with 5.11.0.300.
Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kdubau picture kdubau  路  3Comments

zulhfreelancer picture zulhfreelancer  路  4Comments

EgorBo picture EgorBo  路  3Comments

equalent picture equalent  路  4Comments

kumpera picture kumpera  路  3Comments