c#程序定期把內(nèi)存信息記錄到log日志示例
設(shè)立一個(gè)定時(shí)器tmrMonitor,該定時(shí)器會(huì)在程序運(yùn)行時(shí)不斷把程序的占用內(nèi)存和占用線程數(shù)寫到LOG\MEM目錄下。
我設(shè)置的定時(shí)器間隔是3000毫秒,記錄后的信息可以用來分析一段時(shí)間內(nèi)程序的運(yùn)行狀況,比如內(nèi)存泄漏問題。
/// <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("進(jìn)程標(biāo)識(shí): " + currentProcess.Id.ToString());
sw.WriteLine("進(jìn)程名稱: " + currentProcess.ProcessName.ToString());
sw.WriteLine("占用內(nèi)存: " +
(currentProcess.WorkingSet64 / 1024).ToString() + "KB");
sw.WriteLine("線程數(shù)量: " + currentProcess.Threads.Count.ToString());
sw.WriteLine();
sw.Close();
}
相關(guān)文章
C# FileStream實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了C# FileStream實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析
這篇文章主要介紹了C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
C# Lambda表達(dá)式及Lambda表達(dá)式樹的創(chuàng)建過程
這篇文章主要介紹了C# Lambda表達(dá)式及Lambda表達(dá)式樹的創(chuàng)建過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
C#利用PrintDocument定制打印單據(jù)的小例子
這篇文章主要給大家介紹了關(guān)于C#利用PrintDocument定制打印單據(jù)的小例子,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

