最好有比较详细的代码,谢谢

解决方案 »

  1.   

    GlobalMemoryStatus/GlobalMemoryStatusExExample Code 
    The following code shows a simple use of the GlobalMemoryStatus function.//  Sample output:
    //  The MemoryStatus structure is 32 bytes long.
    //  It should be 32.
    //  78 percent of memory is in use.
    //  There are   65076 total Kbytes of physical memory.
    //  There are   13756 free Kbytes of physical memory.
    //  There are  150960 total Kbytes of paging file.
    //  There are   87816 free Kbytes of paging file.
    //  There are  1fff80 total Kbytes of virtual memory.
    //  There are  1fe770 free Kbytes of virtual memory.#include <windows.h>// Use to change the divisor from Kb to Mb.#define DIV 1024
    // #define DIV 1char *divisor = "K";
    // char *divisor = "";// Handle the width of the field in which to print numbers this way to
    // make changes easier. The asterisk in the print format specifier
    // "%*ld" takes an int from the argument list, and uses it to pad and
    // right-justify the number being formatted.
    #define WIDTH 7void main(int argc, char *argv[])
    {
      MEMORYSTATUS stat;  GlobalMemoryStatus (&stat);  printf ("The MemoryStatus structure is %ld bytes long.\n",
              stat.dwLength);
      printf ("It should be %d.\n", sizeof (stat));
      printf ("%ld percent of memory is in use.\n",
              stat.dwMemoryLoad);
      printf ("There are %*ld total %sbytes of physical memory.\n",
              WIDTH, stat.dwTotalPhys/DIV, divisor);
      printf ("There are %*ld free %sbytes of physical memory.\n",
              WIDTH, stat.dwAvailPhys/DIV, divisor);
      printf ("There are %*ld total %sbytes of paging file.\n",
              WIDTH, stat.dwTotalPageFile/DIV, divisor);
      printf ("There are %*ld free %sbytes of paging file.\n",
              WIDTH, stat.dwAvailPageFile/DIV, divisor);
      printf ("There are %*lx total %sbytes of virtual memory.\n",
              WIDTH, stat.dwTotalVirtual/DIV, divisor);
      printf ("There are %*lx free %sbytes of virtual memory.\n",
              WIDTH, stat.dwAvailVirtual/DIV, divisor);
    }
      

  2.   

    GetProcessMemoryInfoCollecting Memory Usage Information For a Process
    To determine the efficiency of your application, you may want to examine its memory usage. The following sample code uses the GetProcessMemoryInfo function to obtain information about the memory usage of a process.
    #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] );
    }
    The main function obtains a list of processes by using the EnumProcesses function. For each process, main calls the PrintMemoryInfo function, passing the process identifier. PrintMemoryInfo in turn calls the OpenProcess function to obtain the process handle. If OpenProcess fails, the output shows only the process identifier. For example, OpenProcess fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them. Finally, PrintMemoryInfo calls the GetProcessMemoryInfo function to obtain the memory usage information.
      

  3.   

    The GlobalMemoryStatus function obtains information about the computer system's current usage of both physical and virtual memory. VOID GlobalMemoryStatus(
      LPMEMORYSTATUS lpBuffer   // pointer to the memory status structure
    )typedef struct _MEMORYSTATUS { // mst 
        DWORD dwLength;        // sizeof(MEMORYSTATUS) 
        DWORD dwMemoryLoad;    // percent of memory in use 
        DWORD dwTotalPhys;     // bytes of physical memory 
        DWORD dwAvailPhys;     // free physical memory bytes 
        DWORD dwTotalPageFile; // bytes of paging file 
        DWORD dwAvailPageFile; // free bytes of paging file 
        DWORD dwTotalVirtual;  // user bytes of address space 
        DWORD dwAvailVirtual;  // free user bytes 
    } MEMORYSTATUS, *LPMEMORYSTATUS; 
      Header: Declared in winbase.h.
     Import Library: Use kernel32.lib.
      

  4.   

    MEMORYSTATUS mem_stat;           //计算通过Sleep算出,但间隔太小可能引起结果错误
    GlobalMemoryStatus(&mem_stat);   //在不影响结果的情况下可以减短Sleep的间隔 strMemory.Format ("系统内存是%ld MB",mem_stat.dwTotalPhys /(1024*1024));
    m_List.AddString(strMemory);
    strMemory.Format ("系统可用内存是%ld MB",mem_stat.dwAvailPhys /(1024*1024));
    m_List.AddString(strMemory);