MapViewOfFile...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Emmanuel Derriey

    #1

    MapViewOfFile...

    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 (CreateFileMapp ing) 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


  • Jochen Kalmbach

    #2
    Re: MapViewOfFile.. .

    Emmanuel Derriey wrote:
    [color=blue]
    > 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 (CreateFileMapp ing) and
    > re-call MapViewOfFile (last argument is 0, so I map entire file) ;
    > sometimes, it returns NULL and GetLastError returns 8 (not enought
    > space).[/color]

    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_fragmentati on_heap.asp

    The Windows XP Low Fragmentation Heap Algorithm Feature Is Available for
    Windows 2000
    Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.



    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 ?

    Comment

    • Patrick Philippot

      #3
      Re: MapViewOfFile.. .

      Emmanuel Derriey wrote:[color=blue]
      > 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 (CreateFileMapp ing) and
      > re-call MapViewOfFile (last argument is 0, so I map entire file) ;
      > sometimes, it returns NULL and GetLastError returns 8 (not enought
      > space).[/color]

      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 CreateFileMappi ng:

      dwMaximumSizeLo w
      [in] Low-order DWORD of the maximum size of the file mapping object. If
      this parameter and dwMaximumSizeHi gh 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).

      --
      Patrick Philippot - Microsoft MVP [.Net]
      MainSoft Consulting Services

      (replace .xx with .fr when replying by e-mail)


      Comment

      Working...