MapViewOfFile...

  • Thread starter Thread starter Emmanuel Derriey
  • Start date Start date
E

Emmanuel Derriey

Hi !
I have a problem with the MapViewOfFile API.
I use this API to store data in files in my own database system management ;
when I need to store new data, is the file is NOT large enought, I close the
file, reopen and grow it (CreateFileMapping) and re-call MapViewOfFile (last
argument is 0, so I map entire file) ; sometimes, it returns NULL and
GetLastError returns 8 (not enought space). I test it with differents
computers, with 512 Mb RAM to 2Gb RAM, with large swap files, but the
problem seems not to be linked to memory... More over, the memory occupied
by my application is always less that physical memory...
It seems to happen when file is about 250 Gb, but it's not systematic...
More over, it seems to have differents behaviours between Win2000 and WinXP.
Is this a problem due to memory fragmentation ?
Thanks for any help...

Emmanuel Derriey
 
Emmanuel said:
I have a problem with the MapViewOfFile API.
I use this API to store data in files in my own database system
management ; when I need to store new data, is the file is NOT large
enought, I close the file, reopen and grow it (CreateFileMapping) and
re-call MapViewOfFile (last argument is 0, so I map entire file) ;
sometimes, it returns NULL and GetLastError returns 8 (not enought
space).

You already got the answer, didn´t you ?

The problem is that if you want to map the whole file, there must be an
continous range of virtual address to map the file. For many reasons
there is no virtual address range with the desired length in your apps
address space.

Maybe the low fragmentation heap can help you:

Low-fragmentation Heap
http://msdn.microsoft.com/library/en-
us/memory/base/low_fragmentation_heap.asp

The Windows XP Low Fragmentation Heap Algorithm Feature Is Available for
Windows 2000
http://support.microsoft.com/?kbid=816542


You also should try to split the file mapping, so you do not need to map
the whole file. This can be easily done by a wrapper class for the file
mapping. Access the file mapping with this class. Internally you can map
the needed portion of the file if it is not yet mapped.

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/useritems/leakfinder.asp
 
Emmanuel said:
I have a problem with the MapViewOfFile API.
I use this API to store data in files in my own database system
management ; when I need to store new data, is the file is NOT large
enought, I close the file, reopen and grow it (CreateFileMapping) and
re-call MapViewOfFile (last argument is 0, so I map entire file) ;
sometimes, it returns NULL and GetLastError returns 8 (not enought
space).

Could you please give more details (possibly a code excerpt) about the
exact sequence of operations that take place when you decide to destroy
the current file mapping and create a larger one?

Also, look at the documentation of CreateFileMapping:

dwMaximumSizeLow
[in] Low-order DWORD of the maximum size of the file mapping object. If
this parameter and dwMaximumSizeHigh are zero, ***the maximum size of
the file mapping object is equal to the current size of the file
identified by hFile***.

So, if you need to have a MMF larger than the mapped file, you must
explicitly set the size of the MMF (GetFileSize + extra bytes).
 
Back
Top