public void TestFromProcess() { // Generate MemorySnapshot for Notepad. MemorySnapshot memorySnapshot = MemorySnapshot.FromProcess(notepad.Id); // Generate the oracle for comparison. Dictionary <string, long> oracle = GenerateOracle(notepad.Id); // Do a comparison of all the properties. VerifyProperties(memorySnapshot, oracle); }
public static void GetMemoryShapshot(MemorySnapshotCmdletBase cmdlet, int[] processIds) { if (null != processIds && 0 < processIds.Length) { foreach (int processId in processIds) { MemorySnapshot snapshot = MemorySnapshot.FromProcess(processId); cmdlet.WriteObject(cmdlet, snapshot); } } }
public void TestCompareTo() { // Generate a memory snapshot. MemorySnapshot memorySnapshot = MemorySnapshot.FromProcess(notepad.Id); // Generate a second MemorySnapshot and get a diff. MemorySnapshot latestSnap = MemorySnapshot.FromProcess(notepad.Id); MemorySnapshot diff = latestSnap.CompareTo(memorySnapshot); // Manipulate the oracle to contain the same diff. Dictionary <string, long> diffOracle = GenerateDiffOracle(memorySnapshot, latestSnap); // Compare to verify data. VerifyProperties(diff, diffOracle); }
public void TestFromFile() { string filePath = @"C:\testSnap.xml"; // Serialize MemorySnapshot to file. MemorySnapshot memorySnapshot = MemorySnapshot.FromProcess(notepad.Id); memorySnapshot.ToFile(filePath); // Generate the oracle for comparison. Dictionary <string, long> oracle = GenerateOracle(notepad.Id); // Call from file to load data from file. MemorySnapshot fileSnapshot = MemorySnapshot.FromFile(filePath); // Compare to data in oracle. VerifyProperties(fileSnapshot, oracle); }
public void TestToFile() { string filePath = @"C:\testSnap.xml"; // Store call ToFile to log memorySnapshot to file. MemorySnapshot memorySnapshot = MemorySnapshot.FromProcess(notepad.Id); memorySnapshot.ToFile(filePath); // Generate the oracle for comparison. Dictionary <string, long> oracle = GenerateOracle(notepad.Id); // Go through xml nodes and compare to data in oracle. XmlDocument xmlDoc = new XmlDocument(); using (Stream s = new FileInfo(filePath).OpenRead()) { try { xmlDoc.Load(s); } catch (XmlException) { throw new XmlException("MemorySnapshot file \"" + filePath + "\" could not be loaded."); } } // Grab memory stats. Assert.Equal(oracle["GdiObjectCount"], DeserializeNode(xmlDoc, "GdiObjectCount")); Assert.Equal(oracle["HandleCount"], DeserializeNode(xmlDoc, "HandleCount")); Assert.Equal(oracle["PageFileBytes"], DeserializeNode(xmlDoc, "PageFileBytes")); Assert.Equal(oracle["PageFilePeakBytes"], DeserializeNode(xmlDoc, "PageFilePeakBytes")); Assert.Equal(oracle["PoolNonpagedBytes"], DeserializeNode(xmlDoc, "PoolNonpagedBytes")); Assert.Equal(oracle["PoolPagedBytes"], DeserializeNode(xmlDoc, "PoolPagedBytes")); Assert.Equal(oracle["ThreadCount"], DeserializeNode(xmlDoc, "ThreadCount")); Assert.Equal(oracle["UserObjectCount"], DeserializeNode(xmlDoc, "UserObjectCount")); Assert.Equal(oracle["VirtualMemoryBytes"], DeserializeNode(xmlDoc, "VirtualMemoryBytes")); Assert.Equal(oracle["VirtualMemoryPrivateBytes"], DeserializeNode(xmlDoc, "VirtualMemoryPrivateBytes")); Assert.Equal(oracle["WorkingSetBytes"], DeserializeNode(xmlDoc, "WorkingSetBytes")); Assert.Equal(oracle["WorkingSetPeakBytes"], DeserializeNode(xmlDoc, "WorkingSetPeakBytes")); Assert.Equal(oracle["WorkingSetPrivateBytes"], DeserializeNode(xmlDoc, "WorkingSetPrivateBytes")); }
public void TestToFromFile() { string filePath = @"C:\testSnapCollection.xml"; MemorySnapshotCollection collection = new MemorySnapshotCollection(); // The following is for testing purposes and not representative use of the MemorySnapshotCollection class. MemorySnapshot ms1 = MemorySnapshot.FromProcess(notepad.Id); collection.Add(ms1); MemorySnapshot ms2 = MemorySnapshot.FromProcess(notepad.Id); collection.Add(ms2); MemorySnapshot ms3 = MemorySnapshot.FromProcess(notepad.Id); collection.Add(ms3); // Serialize MemorySnapshot to file. collection.ToFile(filePath); // Call from file to load data from file. MemorySnapshotCollection fileCollection = MemorySnapshotCollection.FromFile(filePath); // Verify Count. Assert.Equal(fileCollection.Count, collection.Count); // Generate Diffs for comparison. MemorySnapshot diff1 = ms1.CompareTo(fileCollection[0]); MemorySnapshot diff2 = ms2.CompareTo(fileCollection[1]); MemorySnapshot diff3 = ms3.CompareTo(fileCollection[2]); // Generate expected Diff results. Dictionary <string, long> diffOracle = GenerateDiffOracle(); // Verify Diffs are as expected. VerifyDiff(diff1, diffOracle); VerifyDiff(diff2, diffOracle); VerifyDiff(diff3, diffOracle); }
public void Run(int detectionCycles, int actionsPerCycle) { //TODO1: Launch memorysnapshot monitor as a seperate process so as not to incur extra load. Type type = this.GetType(); LeakTest leakTest = (LeakTest)Activator.CreateInstance(type); // Create new memory snapshot collection and get a handle to the process. MemorySnapshotCollection collection = new MemorySnapshotCollection(); Process process = Process.GetCurrentProcess(); //TODO2: Add GC cleanup / get ready logic. leakTest.Initialize(); collection.Add(MemorySnapshot.FromProcess(process.Id)); // Rinse and repeat the following as requested by the user. for (int i = 0; i < detectionCycles; i++) { for (int j = 0; j < actionsPerCycle; j++) { // Perform and undo action followed by a snapshot. leakTest.PerformAction(); leakTest.UndoAction(); } collection.Add(MemorySnapshot.FromProcess(process.Id)); } // Log collection to file. string filePath = Path.Combine(Environment.CurrentDirectory, @"snapshots.xml"); collection.ToFile(filePath); TestLog log = new TestLog("LeakLog"); log.LogFile(filePath); log.Close(); }
public static void Main(string[] args) { CommandLineArguments a = Init(args); if (a == null) { PrintUsage(); return; } if (a.ProcessName != null) { Process[] processlist = Process.GetProcesses(); int processFound = 0; foreach (Process p in processlist) { if (string.Equals(a.ProcessName, p.ProcessName, StringComparison.OrdinalIgnoreCase)) { processFound++; a.Pid = p.Id; } } if (processFound == 0) { Console.WriteLine("ProcessName not found. Starting new instance..."); Process p = new Process(); p.StartInfo.FileName = a.ProcessName; p.Start(); a.Pid = p.Id; p.WaitForInputIdle(); } if (processFound > 1) { Console.WriteLine("Found multiple processes with the same name. Please specify a /pid."); return; } } Console.WriteLine("Hit \"Ctrl^C\" to exit."); Console.WriteLine("Attaching to Process ID: " + a.Pid); Console.WriteLine("Generating memory snapshots..."); Console.CancelKeyPress += new ConsoleCancelEventHandler(ConsoleOnCancelKeyPress); MemorySnapshotCollection msc = new MemorySnapshotCollection(); if (a.InitialDelay != null) { Thread.Sleep(a.InitialDelay.Value); } PrintHeader(); MemorySnapshot ms; while (true) { if (exitLoop == true) { break; } try { ms = MemorySnapshot.FromProcess(a.Pid.Value); } catch (ArgumentException e) { Console.WriteLine("Process no longer avilable. Terminating MemoryTracer..."); return; } PrintToConsole(ms); if (a.SaveTo != null) { msc.Add(ms); } Thread.Sleep(a.Interval.Value); } if (a.SaveTo != null) { msc.ToFile(a.SaveTo); Console.WriteLine("MemorySnapshots saved to: " + a.SaveTo); } }