среда, 11 июня 2014 г.

MEMORY LEAK DETECTION

TestApi provides a simple memory-leak-detection mechanism, which takes memory snapshots of an executing process and then processes the snapshots with arbitrarily complex leak-detection algorithms, as the following figure shows.
//
// Taking two memory snapshots of Notepad and comparing them for leaks
//

// Start an instance of Notepad.exe, get its PID, and take a memory snapshot
Process p = Process.Start("notepad.exe");
p.WaitForInputIdle(5000);
int pid = p.Id;
Thread.Sleep(3000);
MemorySnapshot s1 = MemorySnapshot.FromProcess(pid);

//
// Perform operations that may cause a leak...
//
Thread.Sleep(2000);

// Capture a second snapshot
MemorySnapshot s2 = MemorySnapshot.FromProcess(pid);

// Compare the two memory snapshots and generate a diff.
// Then display the diff to the console.
MemorySnapshot diff = s2.CompareTo(s1);

Console.WriteLine("Memory diff for process with pid {0}", pid);
Console.WriteLine("Start time:          {0}", s1.Timestamp);
Console.WriteLine("End time:            {0}", s2.Timestamp);
Console.WriteLine("GDI Object Count:    {0}", diff.GdiObjectCount);
Console.WriteLine("Handle Count:        {0}", diff.HandleCount);
Console.WriteLine("PageFile Bytes:      {0}", diff.PageFileBytes);
Console.WriteLine("PageFile Peak Bytes: {0}", diff.PageFilePeakBytes);
Console.WriteLine("Pool Nonpaged Bytes: {0}", diff.PoolNonpagedBytes);
Console.WriteLine("Pool Paged Bytes:    {0}", diff.PoolPagedBytes);
Console.WriteLine("Thread Count:        {0}", diff.ThreadCount);
Console.WriteLine("User Object Count:   {0}", diff.UserObjectCount);
Console.WriteLine("VM Bytes:            {0}", diff.VirtualMemoryBytes);
Console.WriteLine("VM Private Bytes:    {0}", diff.VirtualMemoryPrivateBytes);
Console.WriteLine("WS Bytes:            {0}", diff.WorkingSetBytes);
Console.WriteLine("WS Peak Bytes:       {0}", diff.WorkingSetPeakBytes);
Console.WriteLine("WS Private Bytes:    {0}", diff.WorkingSetPrivateBytes);

// Close the process.
p.CloseMainWindow();
p.Close();

Комментариев нет:

Отправить комментарий