因此,我建立了一个玩具项目来验证这个假设:
//玩具示例展示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
}
}
实际上,结果证实了内存泄漏来自Speech.Synthesizer
Speaking...
内存:42184 KB
正在说话... 内存:42312 KB
正在说话... 内存:42440 KB
正在说话... 内存:42568 KB
正在说话... 内存:42696 KB
正在说话... 内存:42824 KB
正在说话... 内存:43016 KB
正在说话... 内存:43372 KB
我在谷歌上搜索发现许多人遇到了同样的问题: 1: SpeechSynthesizer中持续的内存泄漏 2: http://connect.microsoft.com/VisualStudio/feedback/details/664196/system-speech-has-a-memory-leak
但不幸的是,我没有找到任何解决方法。由于这个问题已经很久以前就被问过了,所以我想问一下它是否已经被解决了?
非常感谢。
更新:
似乎当我切换到使用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();
}
}
内存:32044 KB
正在说话... 内存:32044 KB
正在说话... 内存:32044 KB
正在说话... 内存:32044 KB
正在说话... 内存:32044 KB
正在说话... 内存:32044 KB
正在说话... 内存:32044 KB
正在说话... 内存:32044 KB