如何获得当前程序使用的物理内存的大小!请高手指点!

解决方案 »

  1.   

    using   System.Runtime.InteropServices;     
        
      [DllImport("kernel32")]     
      public   static   extern   void   GlobalMemoryStatus(ref   MEMORY_INFO   meminfo);     
        
      //定义内存的信息结构     
      [StructLayout(LayoutKind.Sequential)]     
      public   struct   MEMORY_INFO     
      {     
      public   uint   dwLength;     
      public   uint   dwMemoryLoad;     
      public   uint   dwTotalPhys;     
      public   uint   dwAvailPhys;     
      public   uint   dwTotalPageFile;     
      public   uint   dwAvailPageFile;     
      public   uint   dwTotalVirtual;     
      public   uint   dwAvailVirtual;     
      }     
        
        
      //调用GlobalMemoryStatus函数获取内存的相关信息     
      MEMORY_INFO   MemInfo;     
      MemInfo   =   new   MEMORY_INFO();     
      GlobalMemoryStatus(ref   MemInfo);     
      MemoryLoad.Text   =   MemInfo.dwMemoryLoad.ToString()+"%的内存正在使用";     
      TotalPhys.Text   =   "物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节";     
      AvailPhys.Text   =   "可使用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节";     
      TotalPageFile.Text   =   "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString()+"字节";     
      AvailPageFile.Text   =   "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString()+"字节";     
      TotalVirtual.Text   =   "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节";     
      AvailVirtual.Text   =   "未用虚拟内存有"+MemInfo
      

  2.   

    文章来自
    http://topic.csdn.net/t/20060220/15/4566076.html
      

  3.   

    http://www.msproject.cn/Article/GetHardwareInformation.aspx 里面信息很多,忘记有没有内存相应的信息了
      

  4.   

    ref:http://pxy.113317.com/n601c36.shtml
      

  5.   

    #include <windows.h>
    #include <stdio.h>
    #include "psapi.h"void PrintMemoryInfo( DWORD processID )
    {
        HANDLE hProcess;
        PROCESS_MEMORY_COUNTERS pmc;    // Print the process identifier.    printf( "\nProcess ID: %u\n", processID );    // Print information about the memory usage of the process.    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                        PROCESS_VM_READ,
                                        FALSE, processID );
        if (NULL == hProcess)
            return;    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
        {
            printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
            printf( "\tPeakWorkingSetSize: 0x%08X\n", 
                      pmc.PeakWorkingSetSize );
            printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
            printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", 
                      pmc.QuotaPeakPagedPoolUsage );
            printf( "\tQuotaPagedPoolUsage: 0x%08X\n", 
                      pmc.QuotaPagedPoolUsage );
            printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", 
                      pmc.QuotaPeakNonPagedPoolUsage );
            printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", 
                      pmc.QuotaNonPagedPoolUsage );
            printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
            printf( "\tPeakPagefileUsage: 0x%08X\n", 
                      pmc.PeakPagefileUsage );
        }    CloseHandle( hProcess );
    }void main( )
    {
        // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the memory usage for each process    for ( i = 0; i < cProcesses; i++ )
            PrintMemoryInfo( aProcesses[i] );
    }
      

  6.   

    hdt(倦怠):哥们,你的这个是C++的程序吧。我看不懂啊。我需要的是C#的。给个思路就行,当然越详细越好了。
      

  7.   

    一般都是Api类别的,语言的差异不是问题
      

  8.   

    class Class1
    {
    [DllImport("psapi.dll")]
    public static extern int GetProcessMemoryInfo( System.IntPtr hProcess, ref PROCESS_MEMORY_COUNTERS ppsmemCounters, int cb );

    [StructLayout(LayoutKind.Sequential)]
    public struct PROCESS_MEMORY_COUNTERS 
    {
    public int cb; 
    public int 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;
    }
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: 在此处添加代码以启动应用程序
    //
    System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
    PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS(); int bs = Class1.GetProcessMemoryInfo( p.Handle , ref pmc , Marshal.SizeOf( pmc.GetType()  ));

    }
      

  9.   

    非常谢谢hdt(倦怠) 。请问如果不使用API,只是使用.NET支持的函数和方法,能实现上面我说的吗?
      

  10.   

    Process 类如下属性
    NonpagedSystemMemorySize 获取分配给此进程的未分页的系统内存大小。 
    PagedMemorySize 获取分页的内存大小。 
    PagedSystemMemorySize 获取分页的系统内存大小。 
    PeakPagedMemorySize 获取峰值分页内存大小。 
    PeakVirtualMemorySize 获取峰值虚拟内存大小。 
    PrivateMemorySize 
      

  11.   

    Process prc= Process.GetCurrentProcess();
    proc.NonpagedSystemMemorySize()即为当前进程的物理内存大小