c#程序定期把内存信息记录到log日志示例
设立一个定时器tmrmonitor,该定时器会在程序运行时不断把程序的占用内存和占用线程数写到log\mem目录下。
我设置的定时器间隔是3000毫秒,记录后的信息可以用来分析一段时间内程序的运行状况,比如内存泄漏问题。
/// <summary>
/// timer组件tmrmonitor的tick事件
/// </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")) //需要system.io
{
directory.createdirectory(logaddress + "\\mem");
}
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();
}
上一篇: Android UI效果之绘图篇(四)
下一篇: treeview递归绑定的两种方法