Noah;
Noah;
Explore posts from servers
CC#
Created by Noah; on 1/31/2024 in #help
Memory leak problem with System.Speech.Recognition && Microsoft.Speech.Recognition
Description: When running the speech recognition using 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 withMicrosoft.Speech.Recognition and is also observed in 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;
}
}
}
22 replies