Constant Memory Leak in SpeechSynthesizer

五迷三道 提交于 2019-12-04 03:16:13

This is a know issue in the Speak method. A structure called SPVTEXTFRAG gets created and never destroyed.

Details here : http://connect.microsoft.com/VisualStudio/feedback/details/664196/system-speech-has-a-memory-leak

I can confirm this observation. I was pulling my hair out trying fo figure out where my program was leaking and it is the .SPEAK method in System.speech

I have converted an app that used the COM-based Speech objects to use the new System.Speech .Net library in .Net 3.5. Sounded like the right way to move forward to using all manged code within the .Net app. The app suddenly had a small mem leak.

I broke this down into 2 simple apps that convert "this is a test" to a WAV file of spoken words. One uses the COM-based speech objects, the other uses System.Speech. I ran them for 24 hours, each creating the WAV about 200,000 times.

COM based speech objects: no mem leak. Mem usage of app peaked at 13MB after about 40 minutes

System.speech: slow leak, nice and linear. Ran from about 14MB to 45MB in 24 hours

SendAsync() from the Ping also leaks. The solution there is to cast the sender as IDisposable first. So maybe the following also works here.

((IDisposable)spk).Dispose();

I can give you a really simple answer for your question: Make the SpeechSynthesizer static!!!

I am quite sure that this will solve your issue.

Also - a tip ==>> every time you code, and you have a resource... use it as static and your life would be better!

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