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); 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> /// Reads the value from the stream. First the buffer is filled from the stream by the data's size in bytes, then the data is copied. /// </summary> /// <typeparam name="T">Type of data to read.</typeparam> /// <param name="input">Stream to read from.</param> /// <returns>The read value.</returns> public unsafe T Read <T>(Stream input) where T : struct { int size = MemoryInterop.SizeOfInline <T>(); //Fill buffer...validate we read t he expected # of bytes if (ReadBytes(input, size) != size) { return(default(T)); } //Copy data return(MemoryInterop.ReadInline <T>((void *)m_pinHandle.AddrOfPinnedObject())); }
/// <summary> /// Casts the by-ref value into a pointer. /// </summary> /// <typeparam name="T">Struct type.</typeparam> /// <param name="src">By-ref value.</param> /// <returns>Pointer to the value.</returns> public static unsafe IntPtr AsPointer <T>(ref T src) where T : struct { return(MemoryInterop.AsPointerInline <T>(ref src)); }
/// <summary> /// Computes the size of the struct type. /// </summary> /// <typeparam name="T">Struct type</typeparam> /// <returns>Size of the struct in bytes.</returns> public static unsafe int SizeOf <T>() where T : struct { return(MemoryInterop.SizeOfInline <T>()); }
/// <summary> /// Clears the memory to the specified value. /// </summary> /// <param name="memoryPtr">Pointer to the memory.</param> /// <param name="clearValue">Value the memory will be cleared to.</param> /// <param name="sizeInBytesToClear">Number of bytes, starting from the memory pointer, to clear.</param> public static unsafe void ClearMemory(IntPtr memoryPtr, byte clearValue, int sizeInBytesToClear) { MemoryInterop.MemSetUnalignedInline((void *)memoryPtr, clearValue, (uint)sizeInBytesToClear); }
/// <summary> /// Casts an underlying value type to an enum type, WITHOUT first casting the value to an Object. So this avoid boxing the value. /// </summary> /// <typeparam name="V">Underlying value type.</typeparam> /// <typeparam name="T">Enum type.</typeparam> /// <param name="value">Value to cast.</param> /// <returns>Enum value.</returns> public static T CastToEnum <V, T>(V value) where V : struct, IComparable, IFormattable, IConvertible where T : struct, IComparable, IFormattable, IConvertible { return(MemoryInterop.As <V, T>(ref value)); }