.Net Speech.Synthesizer中的内存泄漏?

JXI*_*ITC 8 .net c# memory-leaks sapi speech-synthesis

我在申请中发现了连续泄漏.在使用内存分析器进行检查后,我发现该课程是Microsoft Speech.Synthesizer中的一些对象

所以我建立了一个玩具项目来验证这个假设:

//在Speech.Synthesizer对象中显示内存泄漏的玩具示例

static void Main(string[] args)
{
    string text = "hello world. This is a long sentence";
    PromptBuilder pb = new PromptBuilder();
    pb.StartStyle(new PromptStyle(PromptRate.ExtraFast));
    pb.AppendText(text);
    pb.EndStyle();
    SpeechSynthesizer tts = new SpeechSynthesizer();

while (true)
{
    //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"));

    //tts.Dispose();    //also this doesn't work as well
    //tts = null;

    GC.Collect();   //a little help, but still leaks
}
}
Run Code Online (Sandbox Code Playgroud)

结果实际证实内存泄漏来自Speech.Synthesizer

Speaking...
Run Code Online (Sandbox Code Playgroud)

内存:42184 KB

说到......内存:42312 KB

说到......内存:42440 KB

说到......内存:42568 KB

说到......内存:42696 KB

说到......内存:42824 KB

说到......内存:43016 KB

说到......内存:43372 KB

我用Google搜索并发现许多其他人遇到了同样的问题:1: SpeechSynthesizer 2中的常量内存泄漏:http: //connect.microsoft.com/VisualStudio/feedback/details/664196/system-speech-has-a-memory -泄漏

但遗憾的是我没有找到任何解决方案.由于很久以前就已经问到了这个问题,所以我想问一下它是否已经解决了?

非常感谢.

更新:

似乎在我切换到使用SAPI COM dll而不是.Net Speech.Synthesizer包时(虽然基本上它们是相同的东西),内存停止泄漏.

为什么两个调用行为(SAPI dll vs .net Speech package)具有不同的内存行为?因为后者似乎只是前SAPI dll的包装.

    static void Test2()
{
    //SAPI COM component this time
    SpeechLib.SpVoiceClass tts = new SpeechLib.SpVoiceClass();
    tts.SetRate(5);
    string text = "hello world. This is a long sentence";
    //tts.Speak("helloWorld", SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
while (true)
{

    Console.WriteLine("Speaking...");
    tts.Speak(text, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);

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

    GC.Collect();
}
Run Code Online (Sandbox Code Playgroud)

}

内存:32044 KB

说到......内存:32044 KB

说到......内存:32044 KB

说到......内存:32044 KB

说到......内存:32044 KB

说到......内存:32044 KB

说到......内存:32044 KB

说到......内存:32044 KB

JXI*_*ITC 4

最终解决方案:

谷歌搜索相关关键字告诉我这实际上是微软的一个错误。

似乎当我切换到使用 SAPI COM dll 而不是 .Net Speech.Synthesizer 包(尽管本质上它们是相同的东西)时,内存停止泄漏。