设立一个定时器tmrMonitor,该定时器会在程序运行时不断把程序的占用内存和占用线程数写到LOG/MEM目录下。
我设置的定时器间隔是3000毫秒,记录后的信息可以用来分析一段时间内程序的运行状况,比如内存泄漏问题。
LogAddress = String.Concat(LogAddress, "//MEM//",
DateTime.Now.Year, '-', DateTime.Now.Month, '-',
DateTime.Now.Day, "_mem.log");
//需要 System.Diagnostics;
Process currentProcess = Process.GetCurrentProcess();
StreamWriter sw = new StreamWriter(LogAddress, true);
sw.WriteLine('[' + DateTime.Now.ToString() + ']');
sw.WriteLine("进程标识: " + currentProcess.Id.ToString());
sw.WriteLine("进程名称: " + currentProcess.ProcessName.ToString());
sw.WriteLine("占用内存: " +
(currentProcess.WorkingSet64 / 1024).ToString() + "KB");
sw.WriteLine("线程数量: " + currentProcess.Threads.Count.ToString());
sw.WriteLine();
sw.Close();
}
新闻热点
疑难解答