2009-05-31 46 views
1

我正在研究一個簡單的ASP .NET健康檢查程序,並且遇到了一些障礙。ASP .NET c#遠程檢索非分頁內存使用情況

1)我需要能夠從遠程機器(在同一網絡上)獲得完整的非分頁內存使用情況。我試過使用System.Diganostics.Process.NonpagedSystemMemorySize64,但是我發現內核的非分頁用法將會從這個總數中丟失。下面是我在做什麼的快速示例:

Process[] myprocess = Process.GetProcesses("computername"); 

foreach (Process p in myprocess) 
{ 
nonpaged += p.NonpagedSystemMemorySize64; 
} 

2)我可以用System.Diagnostics.PerformanceCounter但是你只能在本地訪問該類的API在本地解決。是否還有另一個課程可以滿足我的需求?

任何幫助,將不勝感激。

回答

1

我以前用來抓取機器診斷的一種解決方案是使用DLLImport。

See P-Invoke

希望這有助於

皮特

在回答您的評論

當使用DLL的導入必須聲明的函數包裝自己。在下面的代碼中,您可以看到public static extern void,它向編譯器說這是一個外部調用,位於DLLImported kernel32.dll中的名爲GlobalMemoryStatus的函數。 MemoryStatus結構是kernel32 dll中的函數的輸出參數,並返回完全填充。

將其複製到您的代碼中,並閱讀他們應該幫助您理解的註釋。

/// <summary> 
     /// Populates a memory status struct with the machines current memory status. 
     /// </summary> 
     /// <param name="stat">The status struct to be populated.</param> 
     [DllImport("kernel32.dll")] 
     public static extern void GlobalMemoryStatus(out MemoryStatus stat); 


     /// <summary> 
     /// The memory status struct is populated by the GlobalMemoryStatus external dll call to Kernal32.dll. 
     /// </summary> 
     public struct MemoryStatus 
     { 
      public uint Length; 
      public uint MemoryLoad; 
      public uint TotalPhysical; 
      public uint AvailablePhysical; 
      public uint TotalPageFile; 
      public uint AvailablePageFile; 
      public uint TotalVirtual; 
      public uint AvailableVirtual; 
     } 

// copy the guts of this method and add it to your own method. 
public void InspectMemoryStatus() 
{ 

MemoryStatus status = new MemoryStatus(); 
      GlobalMemoryStatus(out status); 

    Debug.WriteLine(status.TotalVirtual); 
} 

這應該允許您獲得機器的內存診斷。

+0

感謝皮特, 但是沒有GlobalMemoryStatus或GlobalMemoryStatusex屬性的非分頁使用或整個內核和/或內核分頁的用法,除非我失去了一些東西。 – jw0rd 2009-06-03 01:16:44

+0

正如您在上面看到的,MemoryStatus結構具有內存和頁面文件以及虛擬內存使用等方面的所有信息。 – Peter 2009-06-03 14:18:42