/// <summary> /// De-Serializes a MemorySnapshot instance from xml format. /// </summary> /// <param name="rootNode">The Xml Node from which the MemorySnapshot is to be de-serialized</param> /// <returns></returns> internal static MemorySnapshot Deserialize(XmlNode rootNode) { MemorySnapshot memorySnapshot = new MemorySnapshot(); memorySnapshot.GdiObjectCount = (long)DeserializeNode(rootNode, "GdiObjectCount"); memorySnapshot.HandleCount = (long)DeserializeNode(rootNode, "HandleCount"); memorySnapshot.PageFileBytes = DeserializeNode(rootNode, "PageFileBytes"); memorySnapshot.PageFilePeakBytes = DeserializeNode(rootNode, "PageFilePeakBytes"); memorySnapshot.PoolNonpagedBytes = DeserializeNode(rootNode, "PoolNonpagedBytes"); memorySnapshot.PoolPagedBytes = DeserializeNode(rootNode, "PoolPagedBytes"); memorySnapshot.ThreadCount = (long)DeserializeNode(rootNode, "ThreadCount"); memorySnapshot.UserObjectCount = (long)DeserializeNode(rootNode, "UserObjectCount"); memorySnapshot.VirtualMemoryBytes = (long)DeserializeNode(rootNode, "VirtualMemoryBytes"); memorySnapshot.VirtualMemoryPrivateBytes = DeserializeNode(rootNode, "VirtualMemoryPrivateBytes"); memorySnapshot.WorkingSetBytes = (long)DeserializeNode(rootNode, "WorkingSetBytes"); memorySnapshot.WorkingSetPeakBytes = (long)DeserializeNode(rootNode, "WorkingSetPeakBytes"); memorySnapshot.WorkingSetPrivateBytes = (long)DeserializeNode(rootNode, "WorkingSetPrivateBytes"); // Grab Timestamp. XmlNode memoryStatNode = rootNode.SelectSingleNode("Timestamp"); if (memoryStatNode == null) { throw new XmlException("MemorySnapshot file is missing value: Timestamp"); } XmlAttribute attribute = memoryStatNode.Attributes["Value"]; memorySnapshot.Timestamp = (DateTime)Convert.ToDateTime(attribute.InnerText, CultureInfo.InvariantCulture); return(memorySnapshot); }
/// <summary> /// Creates a MemorySnapshot instance from data in the specified file. /// </summary> /// <param name="filePath">The path to the memory snapshot file.</param> /// <returns>A MemorySnapshot instance containing memory information recorded in the specified file.</returns> public static MemorySnapshot FromFile(string filePath) { MemorySnapshot memorySnapshot = new MemorySnapshot(); 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. XmlNode rootNode = xmlDoc.DocumentElement; memorySnapshot = Deserialize(rootNode); return(memorySnapshot); }
/// <summary> /// Creates a MemorySnapshot instance for the specified OS process. /// </summary> /// <param name="processId">The ID of the process for which to generate the memory snapshot.</param> /// <returns>A MemorySnapshot instance containing memory information for the specified process, /// at the time of the snapshot.</returns> public static MemorySnapshot FromProcess(int processId) { MemorySnapshot memorySnapshot = new MemorySnapshot(); Process process = Process.GetProcessById(processId); process.Refresh(); PROCESS_MEMORY_COUNTERS_EX counters = MemoryInterop.GetCounters(process.Handle); // Populate memory statistics. memorySnapshot.GdiObjectCount = NativeMethods.GetGuiResources(process.Handle, NativeMethods.GR_GDIOBJECTS); memorySnapshot.HandleCount = process.HandleCount; memorySnapshot.PageFileBytes = counters.PagefileUsage; memorySnapshot.PageFilePeakBytes = counters.PeakPagefileUsage; memorySnapshot.PoolNonpagedBytes = counters.QuotaNonPagedPoolUsage; memorySnapshot.PoolPagedBytes = counters.QuotaPagedPoolUsage; memorySnapshot.ThreadCount = process.Threads.Count; memorySnapshot.UserObjectCount = NativeMethods.GetGuiResources(process.Handle, NativeMethods.GR_USEROBJECTS); memorySnapshot.VirtualMemoryBytes = process.VirtualMemorySize64; memorySnapshot.VirtualMemoryPrivateBytes = counters.PrivateUsage; memorySnapshot.WorkingSetBytes = process.WorkingSet64; memorySnapshot.WorkingSetPeakBytes = process.PeakWorkingSet64; memorySnapshot.WorkingSetPrivateBytes = MemoryInterop.GetPrivateWorkingSet(process); memorySnapshot.Timestamp = DateTime.Now; return(memorySnapshot); }
/// <summary> /// Compares the current MemorySnapshot instance to the specified MemorySnapshot to produce a difference. /// </summary> /// <param name="memorySnapshot">The MemorySnapshot to be compared to.</param> /// <returns>A new MemorySnapshot object representing the difference between the two memory snapshots /// (i.e. the result of the comparison).</returns> public MemorySnapshot CompareTo(MemorySnapshot memorySnapshot) { MemorySnapshot diff = new MemorySnapshot(); diff.GdiObjectCount = GdiObjectCount - memorySnapshot.GdiObjectCount; diff.HandleCount = HandleCount - memorySnapshot.HandleCount; diff.PageFileBytes = new IntPtr(PageFileBytes.ToInt64() - memorySnapshot.PageFileBytes.ToInt64()); diff.PageFilePeakBytes = new IntPtr(PageFilePeakBytes.ToInt64() - memorySnapshot.PageFilePeakBytes.ToInt64()); diff.PoolNonpagedBytes = new IntPtr(PoolNonpagedBytes.ToInt64() - memorySnapshot.PoolNonpagedBytes.ToInt64()); diff.PoolPagedBytes = new IntPtr(PoolPagedBytes.ToInt64() - memorySnapshot.PoolPagedBytes.ToInt64()); diff.ThreadCount = ThreadCount - memorySnapshot.ThreadCount; diff.UserObjectCount = UserObjectCount - memorySnapshot.UserObjectCount; diff.VirtualMemoryBytes = VirtualMemoryBytes - memorySnapshot.VirtualMemoryBytes; diff.VirtualMemoryPrivateBytes = new IntPtr(VirtualMemoryPrivateBytes.ToInt64() - memorySnapshot.VirtualMemoryPrivateBytes.ToInt64()); diff.WorkingSetBytes = WorkingSetBytes - memorySnapshot.WorkingSetBytes; diff.WorkingSetPeakBytes = WorkingSetPeakBytes - memorySnapshot.WorkingSetPeakBytes; diff.WorkingSetPrivateBytes = WorkingSetPrivateBytes - memorySnapshot.WorkingSetPrivateBytes; return(diff); }