关于内存映射文件,在处理大文件效率方面,很多地方提到有不少优越性.可是对于具体的操控都是一略而过,让读者很不得要领.还劳请各位xd发表一下看法最近我需要处理大文件(几百MB~几个GB),就尝试使用了CFile和内存映射文件加以比较.觉得的确Memory File Mapping处理效率较高.由于没有经验,一直对于如何合理的使用File Mapping心里没底.使用内存映射文件,无非以下几个函数:1. CreateFile()
//File Mapping直接相关函数
2. CreateFileMapping()
3. MapViewOfFile()
4. FlushViewOfFile
5. UnmapViewOfFile()
//
6. CloseHandle()这里,如果一个文件8GB,那么我们必然不能一下子全部映射到内存!
下面的解释来自MSDN
================
File Mapping SizeThe size of the file mapping object is independent of the size of the file being mapped. However, if the file mapping object is larger than the file, the system expands the file before CreateFileMapping returns. If the file mapping object is smaller than the file, the system maps only the specified number of bytes from the file.The dwMaximumSizeHigh and dwMaximumSizeLow parameters of CreateFileMapping allow you to specify the number of bytes to be mapped from the file:
When you do not want the size of the file to change (for example, when mapping read-only files), call CreateFileMapping and specify zero for both dwMaximumSizeHigh and dwMaximumSizeLow. Doing this creates a file mapping object exactly the same size as the file. Otherwise, you must calculate or estimate the size of the finished file because file mapping objects are static in size; once created, their size cannot increase or decrease. An attempt to map a file with a length of zero in this manner fails with an error code of ERROR_FILE_INVALID. Programs should test for files with a length of zero and reject such files:The size of a file mapping object backed by a named file is limited by disk space. The size of a file view is limited to the largest available contiguous block of unreserved virtual memory. This is at most 2 GB minus the virtual memory already reserved by the process. Windows Me/98/95:  The size of a file mapping object backed by a named file is also limited by disk space. The size of a file view is limited to the largest available contiguous block of unreserved virtual memory in the shared address space. This is at most 1 GB minus the virtual memory in use by other processes, such as 16-bit Windows-based applications or Win32-based applications using file mapping.
The size of the file mapping object you select controls how far into the file you can "see" with memory mapping. If you create a file mapping object of 500 Kb, you have access only to the first 500 Kb of the file, regardless of the size of the file. Since it costs you no system resources to create a larger file mapping object, create one the size of the file (set the dwMaximumSizeHigh and dwMaximumSizeLow parameters of CreateFileMapping both to zero) even if you do not expect to view the entire file. The cost in system resources comes in creating the views and accessing them.If you want to view a portion of the file that does not start at the beginning of the file, you must create a file mapping object. This object is the size of the portion of the file you want to view plus the offset into the file.那么对于这么大的文件,如果我们需要访问整个文件或者说为了提高访问效率,我们必然需要动态控制File View的范围.请各位针对如何有效使用File mapping发表看法.谢谢!

解决方案 »

  1.   

    http://www.hszxcx.cn/Space/RiZiView.aspx?uid=35472603922977&id=35524011440950
    可以参与一下
      

  2.   

    我不是这个意思,用法我也非常熟悉了。只是对于一些file mapping的技巧和思路不是很有理解。对于file mappnig的使用,不是说它一定就是最好的,关键要会合理有效的管理使用。尤其是对于几个GB以上的文件,对于file view的管理以及如何缓冲文件view都是个技巧。想请有心得的xd谈谈。日后,我摸索之后的心得再和各位分享。楼上xd的这篇文章对于入门file mapping来说的确不错。但对于娴熟的技巧使用而言,它没有帮助。