?
Solved

MapViewOfFile - Unexplainable Behaviour? or Task Manager problems?

Posted on 2003-12-01
4
Medium Priority
?
767 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:bill553030
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9855906
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.

0
 

Author Comment

by:bill553030
ID: 9857142
Ah i see now.

Is there any way of releasing the memory MapViewOfFile has used, without calling UnmapViewOfFile?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 375 total points
ID: 9857266
I don't think that such way exists.
The advantage of file mapping in PAGE_READONLY mode is that when Windows needs additional memory for some purposes, it saves some memory pages into the paging file. Memory loaded by read-only file mapping is not saved in the paging file, it is just overwritten. When this memory is needed again, it is reloaded from file opened by CreateFile and not from paging file. So, one time-consuming operation (copying of memory page to the hard disk) is not executed.
0
 

Author Comment

by:bill553030
ID: 9859742
thanks very much.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question