I found a continuous leakage in my application. After examining using a memory profiler, I found the course is some object from Microsoft Speech.Synthesizer
So I build up a toy project to verify the hypothesis:
//Toy example to show memory leak in Speech.Synthesizer object
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
}
}
And the result actually confirmed the memory leak is from Speech.Synthesizer
Speaking...
Memory: 42184 KB
Speaking... Memory: 42312 KB
Speaking... Memory: 42440 KB
Speaking... Memory: 42568 KB
Speaking... Memory: 42696 KB
Speaking... Memory: 42824 KB
Speaking... Memory: 43016 KB
Speaking... Memory: 43372 KB
I googled it and found many others have come across the same problem: 1: Constant Memory Leak in SpeechSynthesizer 2: http://connect.microsoft.com/VisualStudio/feedback/details/664196/system-speech-has-a-memory-leak
but sadly I didn't find any solution to it. Since its a problem already asked long time ago, so I want to ask if its been solved or not?
Many Thanks.
UPDATE:
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.
Why is the two invoke behavior (SAPI dll vs .net Speech package) have different memory behavior? As the latter seems is just a wrapper to the former 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();
}
}
Memory: 32044 KB
Speaking... Memory: 32044 KB
Speaking... Memory: 32044 KB
Speaking... Memory: 32044 KB
Speaking... Memory: 32044 KB
Speaking... Memory: 32044 KB
Speaking... Memory: 32044 KB
Speaking... Memory: 32044 KB