Click to See Complete Forum and Search --> : what is top size of memory-mapped file?


ephemera
October 19th, 2004, 03:53 AM
Hi,guys...
I had sucessfully used memory-mapped file on small file many times...but yesterday when i used it on 729M file,it failed...the code is following...what should i do to corret the code...thx in advance.

HANDLE hFile = ::CreateFile (pFileName, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if ( hFile == INVALID_HANDLE_VALUE ) {
return FALSE;
}

DWORD dwSizeHi;
m_dwFileSize = ::GetFileSize(hFile, &dwSizeHi);
HANDLE hFileMapping = ::CreateFileMapping(hFile, NULL, PAGE_READONLY,0, 0, NULL);
if ( hFileMapping == NULL ) {
::CloseHandle(hFile);
return FALSE;
}

m_p = (LPBYTE)::MapViewOfFile(hFileMapping, FILE_MAP_READ,
0, 0, 0);
::CloseHandle(hFile);
::CloseHandle(hFileMapping);
if ( m_p == NULL ) {
DWORD dwError = GetLastError();
return FALSE;
}
pFileName was file name,
i mapped the 729M file...m_p == NULL was TRUE;then dwError was 8,it stand for no enough memory...BTW.My memory size was 256M; OS was Windows 2000 server.

Regards,
Ephemera

Boris K K
October 21st, 2004, 04:41 AM
The size of your physical RAM should not be a problem. A disk file mapped read-only or read-write behaves a bit like an additional swap file. According to

http://support.microsoft.com/default.aspx?scid=kb;EN-US;125713

only the available address space of the process (largest contiguous block of it) is the limitaion. On W2K it is 2Gb. I believe the server edition could be configured to allow 3Gb per process.

Does "paging" through the file fit easily into your application? What I mean is instead mapping the whole file into the memory space of your process to use the last 3 parameters of MapViewOfFile to map a portion of it, then unmap it and map the next portion etc.

Amn
October 23rd, 2004, 10:11 AM
Boris, are you saying that one can create a 2Gb big view of 2Gb+ file ? Why does the MapViewOfFile fail then ?

Boris K K
October 26th, 2004, 04:19 AM
I cannot guarantee about creating a 2Gb view because part of your process's address space is used by executable modules and data. But yes, you can create a view smaller than the size of the file. See the MSDN (http://msdn.microsoft.com/library/en-us/fileio/base/mapviewoffile.asp) description of the parameters dwFileOffsetHigh, dwFileOffsetLow, and dwNumberOfBytesToMap. In the code from your first post dwNumberOfBytesToMap is 0, which means map the whole file.

Amn
October 26th, 2004, 05:26 AM
Then I guess it makes sense to view the whole map at once, because kernel paging is significantly faster than swapping the views. Make a view which is say, 512Mb high (is that within limits of available process space ?) ?

Andreas Masur
October 26th, 2004, 12:03 PM
Why does the MapViewOfFile fail then ?
This can always be answered by calling 'GetLastError()'... :cool:

Amn
October 26th, 2004, 01:00 PM
It was. It returns "Not enough memory" ;)