获取进程内存信息 GetProcessMemoryInfo (p/invoke)

  [StructLayout(LayoutKind.Sequential)]

    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();

               /// <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);

 

 

        /// <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;
        }

你可能感兴趣的:(内存,信息)