我们有一个程序是用来控制内存使用量的,根据程序的设置,可以控制内存使用量的多少。
比方说,现在系统内存使用量是200M,程序设置增加300M,那么系统内存使用量就会马上上升到500M。可是这样子增加的内存数是虚拟内存和物理内存的总和,而我们实际上需要的是增加物理内存的使用量,我们在项目测试中需要用到这个,我们想让增加的内存数都是物理内存的增加量,有什么办法来控制物理内存的分配。我们使用的操作系统是windows2000。例如:
现在计算机物理内存使用100M
我们设置增加100M
那么现在物理内存使用量就是200M

解决方案 »

  1.   

    在windows下面如何申请一大块物理内存?
      

  2.   

    申请一块大内存可以用 VirtualAlloc , Win2k pro 最大大概可以分配到 2GB但是这样不知道是否可以使Windows弹出那个内存不足的提示:)
      

  3.   

    VirtualAllocEx(...);
    VirtualLock(...);
      

  4.   

    内存管理本来就是操作系统管理的,使用API分配内存,实际的内存还是由操作系统分配的。如果要直接管理物理内存,那一定是ring0级的,我想API也不一定能直接解决的。Win2K的任务管理器中到有这方面的显示,不知道如何实现的,学习了哦。
      

  5.   

    用AllocateUserPhysicalPages()#pragma comment(lib, "advapi32.lib")
    #define _WIN32_WINNT 0x0502 //windows2003下定义,其他系统参考MSDN
    #include <windows.h>
    #include <stdio.h>#define MEMORY_REQUESTED 10*1024*1024 // request a megabyteBOOL
    LoggedSetLockPagesPrivilege ( HANDLE hProcess,
                                  BOOL bEnable);void _cdecl main()
    {
      BOOL bResult;                   // generic Boolean value
      ULONG_PTR NumberOfPages;        // number of pages to request
      ULONG_PTR NumberOfPagesInitial; // initial number of pages requested
      ULONG_PTR *aPFNs;               // page info; holds opaque data
      PVOID lpMemReserved;            // AWE window
      SYSTEM_INFO sSysInfo;           // useful system information
      int PFNArraySize;               // memory to request for PFN array  GetSystemInfo(&sSysInfo);  // fill the system information structure  printf ("This computer has a page size of %d.\n", sSysInfo.dwPageSize);  // Calculate the number of pages of memory to request.  NumberOfPages = MEMORY_REQUESTED/sSysInfo.dwPageSize;
      printf ("Requesting %d pages of memory.\n", NumberOfPages);  // Calculate the size of the user PFN array.  PFNArraySize = NumberOfPages * sizeof (ULONG_PTR);  printf ("Requesting a PFN array of %d bytes.\n", PFNArraySize);  aPFNs = (ULONG_PTR *) HeapAlloc (GetProcessHeap (), 0, PFNArraySize);  if (aPFNs == NULL) {
        printf ("Failed to allocate on heap.\n");
        return;
      }  // Enable the privilege.  if( ! LoggedSetLockPagesPrivilege( GetCurrentProcess(), TRUE ) ) {
        return;
      }  // Allocate the physical memory.  NumberOfPagesInitial = NumberOfPages;
      bResult = AllocateUserPhysicalPages( GetCurrentProcess(),
                                           &NumberOfPages,
                                           aPFNs );
        
      if( bResult != TRUE ) 
      {
        printf("Cannot allocate physical pages, error %u.\n", GetLastError() );
        return;
      }  if( NumberOfPagesInitial != NumberOfPages ) 
      {
        printf("Allocated only %p pages.\n", NumberOfPages );
        return;
      }  // Reserve the virtual memory.
        
      lpMemReserved = VirtualAlloc( NULL,
                                    MEMORY_REQUESTED,
                                    MEM_RESERVE | MEM_PHYSICAL,
                                    PAGE_READWRITE );  if( lpMemReserved == NULL ) 
      {
        printf("Cannot reserve memory.\n");
        return;
      }  // Map the physical memory into the window.
        
      bResult = MapUserPhysicalPages( lpMemReserved,
                                      NumberOfPages,
                                      aPFNs );  if( bResult != TRUE ) 
      {
        printf("MapUserPhysicalPages failed to map, error %u.\n", GetLastError() );
        return;
      }  // unmap
        
      bResult = MapUserPhysicalPages( lpMemReserved,
                                      NumberOfPages,
                                      NULL );  if( bResult != TRUE ) 
      {
        printf("MapUserPhysicalPages failed to unmap, error %u.\n", GetLastError() );
        return;
      }  // Free the physical pages.  bResult = FreeUserPhysicalPages( GetCurrentProcess(),
                                       &NumberOfPages,
                                       aPFNs );  if( bResult != TRUE ) 
      {
        printf("Cannot free physical pages, error %u.\n", GetLastError() );
        return;
      }  // Free virtual memory.  bResult = VirtualFree( lpMemReserved,
                             0,
                             MEM_RELEASE );
    }/*****************************************************************
       LoggedSetLockPagesPrivilege: a function to obtain, if possible, or
       release the privilege of locking physical pages.   Inputs:       HANDLE hProcess: Handle for the process for which the
           privilege is needed       BOOL bEnable: Enable (TRUE) or disable?   Return value: TRUE indicates success, FALSE failure.*****************************************************************/
    BOOL
    LoggedSetLockPagesPrivilege ( HANDLE hProcess,
                                  BOOL bEnable)
    {
      struct {
        DWORD Count;
        LUID_AND_ATTRIBUTES Privilege [1];
      } Info;  HANDLE Token;
      BOOL Result;  // Open the token.  Result = OpenProcessToken ( hProcess,
                                  TOKEN_ADJUST_PRIVILEGES,
                                  & Token);  if( Result != TRUE ) {
        printf( "Cannot open process token.\n" );
        return FALSE;
      }  // Enable or disable?  Info.Count = 1;
      if( bEnable ) 
      {
        Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
      } 
      else 
      {
        Info.Privilege[0].Attributes = 0;
      }  // Get the LUID.  Result = LookupPrivilegeValue ( NULL,
                                      SE_LOCK_MEMORY_NAME,
                                      &(Info.Privilege[0].Luid));  if( Result != TRUE ) 
      {
        printf( "Cannot get privilege value for %s.\n", SE_LOCK_MEMORY_NAME );
        return FALSE;
      }  // Adjust the privilege.  Result = AdjustTokenPrivileges ( Token, FALSE,
                                       (PTOKEN_PRIVILEGES) &Info,
                                       NULL, NULL, NULL);  // Check the result.  if( Result != TRUE ) 
      {
        printf ("Cannot adjust token privileges, error %u.\n", GetLastError() );
        return FALSE;
      } 
      else 
      {
        if( GetLastError() != ERROR_SUCCESS ) 
        {
          printf ("Cannot enable SE_LOCK_MEMORY privilege, please check the local policy.\n");
          return FALSE;
        }
      }  CloseHandle( Token );  return TRUE;
    }在运行程序之前:win2000->控制面板->管理工具->本地安全设置->本地策略->用户权利指派->内存中锁定页
    添加拥有权利的用户,再注销并以该用户名登录。运行程序之后,打开任务管理器,查看 物理内存可用数,