CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    24

    what is top size of memory-mapped file?

    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.

    Code:
    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
    Last edited by ephemera; October 19th, 2004 at 02:55 AM.

  2. #2
    Join Date
    Aug 2004
    Posts
    294

    Re: what is top size of memory-mapped file?

    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...b;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.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  3. #3
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Re: what is top size of memory-mapped file?

    Boris, are you saying that one can create a 2Gb big view of 2Gb+ file ? Why does the MapViewOfFile fail then ?
    Last edited by Amn; October 23rd, 2004 at 09:14 AM.

  4. #4
    Join Date
    Aug 2004
    Posts
    294

    Re: what is top size of memory-mapped file?

    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 description of the parameters dwFileOffsetHigh, dwFileOffsetLow, and dwNumberOfBytesToMap. In the code from your first post dwNumberOfBytesToMap is 0, which means map the whole file.
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  5. #5
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Re: what is top size of memory-mapped file?

    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 ?) ?

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: what is top size of memory-mapped file?

    Quote Originally Posted by Amn
    Why does the MapViewOfFile fail then ?
    This can always be answered by calling 'GetLastError()'...

  7. #7
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Lightbulb Re: what is top size of memory-mapped file?

    It was. It returns "Not enough memory"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured