I am encountered a problem that writing heavy data endlessly to a output window pane caused "Out of memory" exception of Visual studio 2012 (update 3 & 4). I am using OutputWindowPane::OutputString() to write data.
It seems there is an algorithm to limit the text of output window and it does work. But I found there might be an exception as described below:
After writing heavy data (e.g. 1M chars per line) with a time interval of 100mS for a long time (say >10 minutes), the memory(private working set) of devenv process will be stable at about several MegaBytes. Here, if I select some text shown in the output window pane by mouse then click Ctrl+C, the memory of devenv process will increase sharply and in some cases "Out of memory" exception will be raised. This problem has been watched in all my two computers with a high reproducing rate (>50%). Why selecting text causing such big difference? Is there something I should be careful with the text document associated with output window pane?
My reproduce program is described below:
class Program
{
private static EnvDTE.DTE _dte = null;
private static EnvDTE80.DTE2 _dte2 = null;
private static EnvDTE.OutputWindow _outputWindow = null;
private static OutputWindowPane _sitePane = null;
private static bool _stopFlag = false;
static void Main(string[] args)
{
_dte = (EnvDTE.DTE)Marshal.GetActiveObject(@"VisualStudio.DTE.11.0");
_dte2 = _dte as EnvDTE80.DTE2;
_outputWindow = _dte2.ToolWindows.OutputWindow;
string paneName = "Site1";
try
{
_sitePane = _outputWindow.OutputWindowPanes.Item(paneName);
}
catch (System.Exception)
{
_sitePane = _outputWindow.OutputWindowPanes.Add(paneName);
}
System.Threading.Thread thread = new System.Threading.Thread(WriteOutputPane);
thread.Start();
Console.WriteLine("Press a key to finish");
Console.ReadLine();
_stopFlag = true;
thread.Join();
Console.WriteLine("Done...");
}
static protected void WriteOutputPane()
{
Console.WriteLine("thread started");
string digit = @"0123456789012345"; //16 chars
string msg = "";
const int MsgLength = 1024*1024;
while(msg.Length < MsgLength )
{
msg += digit;
}
int loopIndex = 0;
while (_stopFlag == false)
{
try
{
string formattedLoop = string.Format("{0, 8:D4}::::::::", loopIndex++);
string finalMsg = (formattedLoop + msg).Remove(MsgLength - 1) + Environment.NewLine;
int OuputCnt = 1;
for (int cnt = 0; cnt < OuputCnt; ++cnt)
{
_sitePane.Activate();
_sitePane.OutputString(finalMsg);
//_sitePane.ForceItemsToTaskList();
}
System.Threading.Thread.Sleep(100);
}
catch (Exception ex)
{
Console.WriteLine("-- ignore exception -- ");
Console.WriteLine(" {0}", ex.Message);
}
}
}
}
}Thanks for your help.