title: Memory Leak in ConcurrentQueue<T>?
body: <p>Hi I have a simple class in a .NET Core SDK -&gt; 3.1.409 project for the communication with devices.</p>
  <pre><code>public class WriteCommand
  {
      //Commands is an enumeration. 
      public Commands LaserCommand { get; }
  
      public List&lt;byte&gt; Parameter { get; }
  
      public List&lt;byte&gt; Data { get; }
  
      public WriteCommand(Commands laserCommand, byte[] parameter = null)
      {
          Data = BuildSendData(laserCommand, parameter);
          Parameter = new List&lt;byte&gt;(parameter);
          LaserCommand = laserCommand;
      }
  
      private List&lt;byte&gt; BuildSendData(Commands command, byte[] paramBytes)
      {
          var parameter = paramBytes ?? Array.Empty&lt;byte&gt;();
          int numberOfBytes = parameter.Length + Constants.ADD_TO_PARAMETER; // Defined by protocol        
  
          List&lt;byte&gt; sendData = new List&lt;byte&gt;();
  
          sendData.Add(Constants.PACKET_START_BYTE);
  
          sendData.Add((byte)numberOfBytes);
  
          sendData.Add(Constants.COMMAND_START_BYTE);
  
          sendData.Add((byte)command);
  
          foreach (var param in parameter)
          {
              sendData.Add(param);
          }
  
          sendData.Add(Constants.PACKET_END_BYTE);
  
          byte checksum = new CheckSumCalculator().CalculateCheckSum(sendData);
          sendData.Add(checksum);
  
          return sendData;
      }
  }
  </code></pre>
  <p>I use this class to add to a ConcurrentQueue in one taks like this.</p>
  <pre><code>    public void AddCommand()
      {
          commandsQueue.Enqueue(new WriteCommand(Commands.SetRs232BaudRate));
      }
  </code></pre>
  <p>And in another task I get the command out of the ConcurrentQueue</p>
  <pre><code>    public void SendAndReceiveMessages()
      {
          while (!commandsQueue.IsEmpty)
          {
              if (commandsQueue.TryDequeue(out WriteCommand writeCommand))
              {
                 //Do something                    
              }
          }
      }
  </code></pre>
  <p>In my progam I habe 6 devices to communicate within an interval of one second. Each device has it's own communication class.</p>
  <p>When the program run for a while (more than 2 days) I see an increase of the needed memory.</p>
  <p>I Check this with the a memory profiler and see a memory leak:</p>
  <ul>
  <li>WriteCommand</li>
  <li>ConcurrentQueueSegment+Slot</li>
  </ul>
  <p>This is only <a href="https://stackoverflow.com/questions/14041373/memory-leak-with-concurrentqueue">articel</a>I found.</p>
  <p>You can find the example code <a href="https://github.com/MichaelInsberg/Example" rel="nofollow noreferrer">here</a></p>
  <p>Does anyone know this problem?</p>
  <p>Greetings Mike</p>