Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, lpFileMappigAttributes As Any, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
  Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
  Private Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long
  Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
      
   
  Private Const FILE_MAP_ALL_ACCESS = &HF001F
  Private Const PAGE_READWRITE = &H4
Private Sub Form_Load()Build_Memory "memory_001"End Sub
      
           
  Function Build_Memory(strname As String) As Boolean
   
        
  hFile = CreateFileMapping(-1, ByVal 0&, PAGE_READWRITE, 0&, 65535, strname)
  
   
  If hFile Then
          
          ptrShare = MapViewOfFile(hFile, FILE_MAP_ALL_ACCESS, 0&, 0&, 0&)
          
  Else
          MsgBox "Unable   to   get   memory   map   handle."
          Build_Memory = False
          Exit Function
  End If
  
  ' 建立 memory 成功后 , "马上" 用 UnmapViewOfFile() 把内存释放掉
  ' 结果根本释放不掉
  ' 因为 G1 总是 = 0 ,而 Err.LastDllError 则传回错误号码 = 487
   
   
  G1 = UnmapViewOfFile(ptrShare)  If Err.LastDllError = 487 Then Add = "试图访问无效的地址"  If G1 = 0 Then MsgBox ("内存 释放失败 (没有释放掉) >> " & Add)  G2 = CloseHandle(hFile)
  If G2 = 0 Then MsgBox ("hFile 释放失败")
  
  '  CreateFileMapping(-1, ByVal 0&, PAGE_READWRITE, 0&, 655350000, strname)
  '
  ' 最直接的观察方式就是把 65535 改成 65535000 或 655350000. 我的内存耗用量直接从 1G 暴涨到 1.6G
  ' 接下来呼叫 UnmapViewOfFile(ptrShare) , 因为根本没有释放掉 ,
  ' 所以内存耗用根本无法从 1.6 G 降到原始的 1 G 以下
  '
  '

          

  End Function
   '=======================
' 请帮帮忙 谢谢

解决方案 »

  1.   

    ' 抱歉 上面排版失败 , 重写一次 
      Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, lpFileMappigAttributes As Any, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
      Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
      Private Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long
      Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
          
       
      Private Const FILE_MAP_ALL_ACCESS = &HF001F
      Private Const PAGE_READWRITE = &H4  '//========
               
      Function Build_Memory(strname As String) As Boolean
       
            
      hFile = CreateFileMapping(-1, ByVal 0&, PAGE_READWRITE, 0&, 65535, strname)
      
       
      If hFile Then
              
              ptrShare = MapViewOfFile(hFile, FILE_MAP_ALL_ACCESS, 0&, 0&, 0&)
              
      Else
              MsgBox "Unable   to   get   memory   map   handle."
              Build_Memory = False
              Exit Function
      End If
      
      '================================
      ' 建立 memory 成功后 , "马上" 用 UnmapViewOfFile() 把内存释放掉
      ' 结果根本释放不掉
      ' 因为 G1 总是 = 0 ,而 Err.LastDllError 则传回错误号码 = 487
      '================================
       
       
      G1 = UnmapViewOfFile(ptrShare)  If Err.LastDllError = 487 Then Add = "试图访问无效的地址"  If G1 = 0 Then MsgBox ("内存 释放失败 (没有释放掉) >> " & Add)  G2 = CloseHandle(hFile)
      If G2 = 0 Then MsgBox ("hFile 释放失败")
      
      '================================
      '  CreateFileMapping(-1, ByVal 0&, PAGE_READWRITE, 0&, 655350000, strname)
      '
      ' 最直接的观察方式就是把上面的 65535 改成 65535000 或 655350000. 我的内存耗用量直接从 1G 暴涨到 1.6G
      ' 接下来呼叫 UnmapViewOfFile(ptrShare) , 因为根本没有释放掉 ,
      ' 所以内存耗用根本无法从 1.6 G 降到原始的 1 G
      '
      '================================
              
      End Function
      '//========
       
      Private Sub Form_Load()     Build_Memory "memory_001"  End Sub
      

  2.   

    G1 = UnmapViewOfFile(byval ptrShare) 搞清楚传值和传地址的区别
      

  3.   

    我刚刚自己有解决了 (去看宣告才发现问题)我是改宣告  ... (lpBaseAddress As Any) As Long 成为       ... (Byval lpBaseAddress As long) As Long ===================
     
    但是   chenhui530 大  请教您另一个问题(1)更正后 
    执行 Build_Memory "memory_002"  
        Build_Memory "memory_003"  
        Build_Memory "memory_004"  
    .....
    UnmapViewOfFile() 完全都没有问题了唯独因为刚刚我直接开到 655350000 名称为 memory_001 占掉了 0.6G 的内存 , 已经无法释放掉   不知道要如何解决呢 (只能重开机解决吗 ?)(2)Err.LastDllError 数值所代表的实质意义 要去哪里查呢 ??
    谢噜