Link to home
Start Free TrialLog in
Avatar of bill553030
bill553030

asked on

MapViewOfFile - Unexplainable Behaviour? or Task Manager problems?

Hello,

While playing about with CreateFileMapping, and MapViewOfFile, i have recently noticed Task manager reporting something very odd.

It would be easier to explain once you have seen my code.

HANDLE hFile = CreateFile ( "Test.abc", GENERIC_READ, FILE_SHARE_READ, NULL,  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
HANDLE hFileMap = CreateFileMapping ( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
LPVOID      pMapping = (LPVOID)MapViewOfFile( hFileMap,  FILE_MAP_READ, 0, 0, 0 );

__asm
{
      mov            esi, pMapping
      mov            ecx, 1000000

copyloop:
      mov           al, byte ptr [esi]
      inc            esi

      dec            ecx
      jnz            copyloop
}
UnmapViewOfFile( pMapping );
CloseHandle( hFileMap );            
CloseHandle( hFile );

Now as you can see, all i do is open the file copy 1 byte into the register 'al', increase the file pointer by 1, then repeat.

Before any data is 'read' in Task Manager shows "mem usage" at 672 K. but after the loop has finished the mem usage is at 1,652K.

I cannot understand why the mem usage has increased. I am not allocating any memory, all I’m doing in copying 1 single byte to a register.

Could any one please offer me an Explanation to why this happens? and if possible how to stop it?

Thanks for reading.
Avatar of AlexFM
AlexFM

The MapViewOfFile function maps a view of a file into the address space of the calling process.
Windows is smart enough to do exactly what you ask it. It keeps file name and range of memory adresses to which this file is mapped. It doesn't use this memory yet. Only if you use this memory, it reads the file and memory usage grows.
To stop it commemt off asm block :-)
If you are interesting in such questions, read Jeffrey Richter's Programming Applications for Microsoft Windows 2000 - this is a best book about Windows programming.

Avatar of bill553030

ASKER

Ah i see now.

Is there any way of releasing the memory MapViewOfFile has used, without calling UnmapViewOfFile?
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks very much.