[StructLayout(LayoutKind.Sequential)]api
public struct PPROCESS_MEMORY_COUNTERS
{
/// <summary>
/// 結構體大小
/// </summary>
public UInt32 cb;
/// <summary>
/// 缺頁中斷次數
/// </summary>
public UInt32 PageFaultCount;
/// <summary>
/// 使用內存高峯
/// </summary>
public UInt32 PeakWorkingSetSize;
/// <summary>
/// 當前使用的內存
/// </summary>
public UInt32 WorkingSetSize;
/// <summary>
/// 使用頁面緩存池高峯
/// </summary>
public UInt32 QuotaPeakPagedPoolUsage;
/// <summary>
/// 使用頁面緩存池
/// </summary>
public UInt32 QuotaPagedPoolUsage;
/// <summary>
/// 使用非分頁緩存池高峯
/// </summary>
public UInt32 QuotaPeakNonPagedPoolUsage;
/// <summary>
/// 使用非分頁緩存池
/// </summary>
public UInt32 QuotaNonPagedPoolUsage;
/// <summary>
/// 使用分頁文件
/// </summary>
public UInt32 PagefileUsage;
/// <summary>
/// 使用分頁文件的高峯
/// </summary>
public UInt32 PeakPagefileUsage;
}緩存
/// <summary>
/// Retrieves the calling thread's last-error code value.
/// </summary>
/// <returns>The return value is the calling thread's last-error code.</returns>
[System.Runtime.InteropServices.DllImport("Kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
internal static extern Int32 GetLastError();ide
/// <summary>
/// Retrieves information about the memory usage of the specified process.
/// </summary>
/// <param name="hProcess">A handle to the process. The handle must have the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right and the PROCESS_VM_READ access right. </param>
/// <param name="lpCreationTime">A pointer to the PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX structure that receives information about the memory usage of the process. </param>
/// <param name="cb">The size of the ppsmemCounters structure, in bytes. </param>
/// <returns>If the function succeeds, the return value is nonzero.</returns>
[System.Runtime.InteropServices.DllImport("Psapi.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true, EntryPoint = "GetProcessMemoryInfo")]
internal static extern int GetProcessMemoryInfo(IntPtr hProcess, out PPROCESS_MEMORY_COUNTERS ppsmemCounters, Int32 cb);spa
/// <summary>
/// Gets the memory information.
/// </summary>
/// <param name="errorCode">The error code.</param>
/// <returns></returns>
public static PPROCESS_MEMORY_COUNTERS GetMemoryInformation(out int errorCode)
{
errorCode = 0;
PPROCESS_MEMORY_COUNTERS memoryInfo;
if (NativeMethods.GetProcessMemoryInfo(HelpViewerProcess.Handle, out memoryInfo, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PPROCESS_MEMORY_COUNTERS))) != 1)
{
errorCode = NativeMethods.GetLastError();
}
return memoryInfo;
}code