Memory leak in .Net Speech.Synthesizer?

柔情痞子 提交于 2019-12-05 04:39:47

Final solution:

Google-ing relevant keywords telling me that it's actually a bug from Microsoft.

Seems like while I switch to use SAPI COM dll rather than .Net Speech.Synthesizer package (although essentially they are the same thing), the memory stops leaking.

I'm not sure all the details on the SpeechSynthesizer, but you could try using the disposable pattern here. Since SpeechSynthesizer implements IDisposable

Your code would look like the following:

while (true)
{
   using (SpeechSynthesizer tts = new SpeechSynthesizer())
   {
      Console.WriteLine("Speaking..."); 
      tts.Speak(pb);

      //Print private working set sieze
      Console.WriteLine("Memory: {0} KB\n",(Process.GetCurrentProcess().PrivateMemorySize64 / 1024).ToString("0"));
   }
}

If you notice this is very similar to Microsoft's example Here

This looks to in fact be a memory leak, Have you tried using the Microsoft.Speech runtime? The syntax looks very similar and they have mentioned it shouldn't have the same issue.

I know it is an old thread, but there is another solution for the problem. Use Microsoft.Speech.Synthesis.SpeechSynthesizer instead of System.Speech.Synthesis.SpeechSynthesizer.

Microsoft.Speech.Synthesis.SpeechSynthesizer is included in the Microsoft Speech Platform - Software Development Kit (SDK) (Version 11) - https://www.microsoft.com/en-us/download/details.aspx?id=27226

This version of the Synthesizer does not have the memory leak.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!