C#實現進程內存信息獲取


using System.Collections.Generic;
using
System.Runtime.InteropServices;
using
System;
using
System.Diagnostics;
staticclassNat
{
   
[StructLayout(LayoutKind.Sequential]
   
struct IO_COUNTERS
   
{
       
public ulong ReadOperationCount;
       
public ulong WriteOperationCount;
       
public ulong OtherOperationCount;
       
public ulong ReadTransferCount;
       
public ulong WriteTransferCount;
       
public ulong OtherTransferCount;
   
}
   
[DllImport("kernel32.dll")]
   
unsafe static extern bool GetProcessIoCounters(IntPtrProcessHandle,out IO_COUNTERS IoCounters);

   
[StructLayout(LayoutKind.Sequential,Size=40)]
   
privatestruct PROCESS_MEMORY_COUNTERS
   
{
       
public uint cb;
       
public uint PageFaultCount;
       
public uint PeakWorkingSetSize;
       
public uint WorkingSetSize;
       
public uint QuotaPeakPagedPoolUsage;
       
public uint QuotaPagedPoolUsage;
       
public uint QuotaPeakNonPagedPoolUsage;
       
public uint QuotaNonPagedPoolUsage;
       
public uint PagefileUsage;
       
public uint PeakPagefileUsage;
   
}

   
[DllImport("psapi.dll",SetLastError=true)]
   
unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess,out PROCESS_MEMORY_COUNTERS*Memcounters,int size);

   
publicstaticclass IO
   
{
       
unsafepublicstaticDictionary<string,ulong>GetALLIO(Process procToRtrivIO)
       
{
            IO_COUNTERS counters
;
           
Dictionary<string,ulong> retCountIoDict =newDictionary<string,ulong>();
           
IntPtr ptr =System.Diagnostics.Process.GetCurrentProcess().Handle;

           
GetProcessIoCounters(ptr,out counters);
            retCountIoDict
.Add("ReadOperationCount", counters.ReadOperationCount);
            retCountIoDict
.Add("WriteOperationCount", counters.WriteOperationCount);
            retCountIoDict
.Add("OtherOperationCount", counters.OtherOperationCount);
            retCountIoDict
.Add("ReadTransferCount", counters.ReadTransferCount);
            retCountIoDict
.Add("WriteTransferCount", counters.WriteTransferCount);
            retCountIoDict
.Add("OtherTransferCount", counters.OtherTransferCount);
           
return retCountIoDict;
           
//return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
           
//    " Mb of data.";

       
}
   
}
   
publicstaticclassMem
   
{
       
unsafe public staticDictionary<string,uint>GetAllMem(Process procToRtrivMem)
       
{

            PROCESS_MEMORY_COUNTERS
*MemCounters;
           
Dictionary<string,uint> retCountMemDict =newDictionary<string,uint>();
           
IntPtr ptr =System.Diagnostics.Process.GetCurrentProcess().Handle;

           
GetProcessMemoryInfo(&ptr,outMemCounters,Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS)));//MemCounters.cb);
            retCountMemDict
.Add("cb",MemCounters->cb);
            retCountMemDict
.Add("PageFaultCount",MemCounters->PageFaultCount);
            retCountMemDict
.Add("PeakWorkingSetSize",MemCounters->PeakWorkingSetSize);
            retCountMemDict
.Add("WorkingSetSize",MemCounters->WorkingSetSize);
            retCountMemDict
.Add("QuotaPeakPagedPoolUsage",MemCounters->QuotaPeakPagedPoolUsage);
            retCountMemDict
.Add("QuotaPagedPoolUsage",MemCounters->QuotaPagedPoolUsage);

            retCountMemDict
.Add("QuotaPeakNonPagedPoolUsage",MemCounters->QuotaPeakNonPagedPoolUsage);
            retCountMemDict
.Add("QuotaNonPagedPoolUsage",MemCounters->QuotaNonPagedPoolUsage);
            retCountMemDict
.Add("PagefileUsage",MemCounters->PagefileUsage);
            retCountMemDict
.Add("PeakPagefileUsage",MemCounters->PeakPagefileUsage);

           
return retCountMemDict;
           
//return  "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
           
//    " Mb of data.";

       
}
   
}
}

 參考:using unsafe code in C# asp.net   http://stackoverflow.com/questions/17207310/using-unsafe-code-in-c-sharp-asp-net

collectiong memory usage information for a processhttp://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx

在C#中調用psapi.dll內置的GetProcessMemoryInfo函數http://social.microsoft.com/Forums/it-IT/650197e0-a21a-4f5e-a974-23f074f52a55/cpsapidllgetprocessmemoryinfo?forum=visualcshartzhchs

ASP.NET(C#)獲取當前計算機CPU內存使用率等相關信息http://luzinwbing.blog.163.com/blog/static/113805840201031093415658/

 不安全代碼只會在使用/unsafe編譯情況下使用  http://lixiaorong223.blog.163.com/blog/static/44011629200993181241924/

wmi獲得進程的虛擬內存與任務管理器中顯示的不一致  http://bbs.csdn.net/topics/260033107

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM