The c program periodically logs memory information to the log logging example
- 2020-06-19 11:33:25
- OfStack
Set a timer, tmrMonitor, that will constantly write the memory and thread count of the program to the LOG\MEM directory while the program is running.
I set the timer interval to 3000 milliseconds, and the recorded information can be used to analyze the health of the program over a period of time, such as memory leaks.
/// <summary>
/// Timer component tmrMonitor the Tick The event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tmrMonitor_Tick(object sender, EventArgs e)
{
string LogAddress = Environment.CurrentDirectory + "\\Log";
if (!Directory.Exists(LogAddress + "\\MEM")) // Need to be System.IO
{
Directory.CreateDirectory(LogAddress + "\\MEM");
}
LogAddress = String.Concat(LogAddress, "\\MEM\\",
DateTime.Now.Year, '-', DateTime.Now.Month, '-',
DateTime.Now.Day, "_mem.log");
// Need to be System.Diagnostics;
Process currentProcess = Process.GetCurrentProcess();
StreamWriter sw = new StreamWriter(LogAddress, true);
sw.WriteLine('[' + DateTime.Now.ToString() + ']');
sw.WriteLine(" Process id: " + currentProcess.Id.ToString());
sw.WriteLine(" Process name: " + currentProcess.ProcessName.ToString());
sw.WriteLine(" Memory footprint: " +
(currentProcess.WorkingSet64 / 1024).ToString() + "KB");
sw.WriteLine(" Thread count: " + currentProcess.Threads.Count.ToString());
sw.WriteLine();
sw.Close();
}