请问各位是如何监视内存变化的?
都有多少种方法?希望您能给出详细点的说明.
谢谢!!!

解决方案 »

  1.   

    MEMORYSTATUS mem;
    HDC hDC;
    HFONT hfnt, hOldFont;
    int x, y;
    RECT rect;
    TCHAR szMsg[256];
    TCHAR szMsg2[16], szMsg3[16]; // Get the available memory info
    mem.dwLength = sizeof(MEMORYSTATUS);
    GlobalMemoryStatus(&mem); // Display it
    hDC = GetDC(hwndMain);
        hfnt = GetStockObject(ANSI_VAR_FONT); // small font
        hOldFont = SelectObject(hDC, hfnt);
    SetBkMode(hDC, TRANSPARENT); // Transparent text x = 5;
    y = 5;
    // Clear the window background with light Yellow
    GetClientRect(hwndMain, &rect);
    FillRect(hDC, &rect, (HBRUSH)(COLOR_INFOBK+1) );
    // Show Physical memory
    FormatBytes(szMsg2, mem.dwAvailPhys);
    FormatBytes(szMsg3, mem.dwTotalPhys - mem.dwAvailPhys);
    wsprintf(szMsg, "RAM:  %s avail, %s used", szMsg2, szMsg3);
    TextOut(hDC, x, (y), szMsg, lstrlen(szMsg));
    // Show Page File
    FormatBytes(szMsg2, mem.dwTotalPageFile - mem.dwAvailPageFile);
    FormatBytes(szMsg3, mem.dwAvailPageFile);
    wsprintf(szMsg, "Paging:  %s used, %s avail", szMsg2, szMsg3);
    TextOut(hDC, x, (y+=15), szMsg, lstrlen(szMsg));
    // Show Virtual memory
    FormatBytes(szMsg2, mem.dwTotalVirtual - mem.dwAvailVirtual);
    FormatBytes(szMsg3, mem.dwAvailVirtual);
    wsprintf(szMsg, "Virtual mem: %s used, %s avail", szMsg2, szMsg3);
    TextOut(hDC, x, (y+=15), szMsg, lstrlen(szMsg)); SelectObject(hDC, hOldFont);
    ReleaseDC(hwndMain, hDC);
    定时调用...
      

  2.   

    这两天我这地望不灵,没有及时提问,请问 9731boy
    您的这段码应该添在什么位置?hwndMain的定义在什么位置?
      

  3.   

    用GlobalMemoryStatus这个API函数可以测试当前内存的大小,包括物理的,虚拟的,可用的、不可用的都能检测。
      

  4.   

    VOID GlobalMemoryStatus(
      LPMEMORYSTATUS lpBuffer   // memory status structure
    );
    这是API函数的定义形式,参数lpBuffer下面类型的结构:
    typedef struct _MEMORYSTATUS { 
        DWORD dwLength; 
        DWORD dwMemoryLoad; 
        SIZE_T dwTotalPhys; 
        SIZE_T dwAvailPhys; 
        SIZE_T dwTotalPageFile; 
        SIZE_T dwAvailPageFile; 
        SIZE_T dwTotalVirtual; 
        SIZE_T dwAvailVirtual; 
    } MEMORYSTATUS, *LPMEMORYSTATUS; MembersdwLength 
    Size, in bytes, of the MEMORYSTATUS data structure. You do not need to set this member before calling the GlobalMemoryStatus function; the function sets it. 
    dwMemoryLoad 
    Windows NT 3.1 – NT 4.0: The percentage of approximately the last 1000 pages of physical memory that is in use. 
    Windows 2000: The approximate percentage of total physical memory that is in use. dwTotalPhys 
    Total size, in bytes, of physical memory. 
    dwAvailPhys 
    Size, in bytes, of physical memory available. 
    dwTotalPageFile 
    Total possible size, in bytes, of the paging file. Note that this number does not represent the actual physical size of the paging file on disk. 
    dwAvailPageFile 
    Size, in bytes, of space available in the paging file. The operating system can enlarge the paging file from time to time. The dwAvailPageFile member shows the difference between the size of current committed memory and the current size of the paging file — it does not show the largest possible size of the paging file. 
    dwTotalVirtual 
    Total size, in bytes, of the user mode portion of the virtual address space of the calling process. 
    dwAvailVirtual 
    Size, in bytes, of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process. 
    详细情况你可以到MSDN上查一下!
      

  5.   

    请大家见仁见智踊跃参加,凡是提出实质性的回答的前辈,都将会奉上20分,
    如9731boy(FAS(飞鸟)是我MM ^o^) , skybblue(天蓝)等前辈。
    这个贴子为了多收集方法先不揭帖。希望大家集思广益。
    ocean1
      

  6.   

    // "\\System\\Threads";          當前線程數
    // "\\System\\Processes";    當前進程數// "\\Memory\\Commit Limit";      當前內存總數
    // "\\Memory\\Committed Bytes"  已用內存總數
    以上是strSysInfo的入口參數STDMETHODIMP CMultiThreadDlg::GetSysInfo(long *plResult, CString strSysInfo)
    {
    CString str;
    PDH_STATUS  pdhStatus;      
    HCOUNTER *pCounterHandle; PDH_FMT_COUNTERVALUE  fmtValue;
    DWORD          ctrType;
       CHAR szPathBuffer[MAXPATH]={'\0'};
    pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);     
    pCounterHandle=(HCOUNTER *)GlobalAlloc(GPTR,sizeof(HCOUNTER));
    strcat(szPathBuffer,strSysInfo);
    pdhStatus = PdhAddCounter(hQuery,szPathBuffer,0,pCounterHandle);
    pdhStatus = PdhCollectQueryData (hQuery);
    pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle,
                                                  PDH_FMT_DOUBLE,
                                                  &ctrType,
                                                  &fmtValue);
    *plResult = (UINT)fmtValue.doubleValue;
    return S_OK;
    }
    我用過了,可以的。