© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
21 replies
Noah;

Memory leak problem with System.Speech.Recognition && Microsoft.Speech.Recognition

Description:
When running the speech recognition using
Microsoft.Speech.Recognition
Microsoft.Speech.Recognition
, there appears to be a memory leak issue. The recognition engine constantly grows in memory during execution, even though it is disposed of correctly. The problem is specifically observed in the following code block:

while (!stoppingToken.IsCancellationRequested && !disposed)
{
    Thread.Sleep(100);
    try
    {
        int read = originStream.Read(buffer, 0, 48000);
        bufferedByteStream.Write(buffer, 0, read);
    }
    catch (IOException ex) when (ex.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
    {
        Console.WriteLine("Connection was forcibly closed by the remote host.");
        bufferedByteStream.Close();
        disposed = true;
    }
}
while (!stoppingToken.IsCancellationRequested && !disposed)
{
    Thread.Sleep(100);
    try
    {
        int read = originStream.Read(buffer, 0, 48000);
        bufferedByteStream.Write(buffer, 0, read);
    }
    catch (IOException ex) when (ex.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
    {
        Console.WriteLine("Connection was forcibly closed by the remote host.");
        bufferedByteStream.Close();
        disposed = true;
    }
}


Context:
- The recognition engine grows in memory while it runs in a separate thread.
- Disposing of the engine at the end of the execution does not resolve the issue.
- The memory growth is evident even with correct disposal practices.

Additional Information:
- The issue happens with
Microsoft.Speech.Recognition
Microsoft.Speech.Recognition
and is also observed in
System.Speech.Recognition
System.Speech.Recognition
.
- The code is part of a background service and runs continuously.

Code Snippet for Reference:
// ... (previous code)

using (SpeechRecognitionEngine engine = SREBuilder.Create(new[] { "1FriendlyDoge", "notanoob600m", "RoyalCrests" }))
{
  engine.SpeechRecognized += (sender, e) => HandleSpeechRecognized(sender, e, userId, guildId);
  engine.SetInputToAudioStream(bufferedByteStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 48000, 16, 2, 192000, 4, null));
  engine.RecognizeAsync(RecognizeMode.Multiple);
    
    while (!stoppingToken.IsCancellationRequested && !disposed)
    {
        Thread.Sleep(100);
        try
        {
            int read = originStream.Read(buffer, 0, 48000);
            bufferedByteStream.Write(buffer, 0, read);
        }
        catch (IOException ex) when (ex.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
        {
            Console.WriteLine("Connection was forcibly closed by the remote host.");
            bufferedByteStream.Close();
            disposed = true;
        }
    }
}
// ... (previous code)

using (SpeechRecognitionEngine engine = SREBuilder.Create(new[] { "1FriendlyDoge", "notanoob600m", "RoyalCrests" }))
{
  engine.SpeechRecognized += (sender, e) => HandleSpeechRecognized(sender, e, userId, guildId);
  engine.SetInputToAudioStream(bufferedByteStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 48000, 16, 2, 192000, 4, null));
  engine.RecognizeAsync(RecognizeMode.Multiple);
    
    while (!stoppingToken.IsCancellationRequested && !disposed)
    {
        Thread.Sleep(100);
        try
        {
            int read = originStream.Read(buffer, 0, 48000);
            bufferedByteStream.Write(buffer, 0, read);
        }
        catch (IOException ex) when (ex.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionReset })
        {
            Console.WriteLine("Connection was forcibly closed by the remote host.");
            bufferedByteStream.Close();
            disposed = true;
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Has anyone every used: System.Speech.Recognition locally with great success?
C#CC# / help
2y ago
Memory leak : WeakReferences
C#CC# / help
17mo ago
Debugging memory leak
C#CC# / help
17mo ago
❔ dataset memory leak
C#CC# / help
3y ago