Discussion:
RAM is not freed even after UnmapViewOfFile on XP SP3
(too old to reply)
Naveen
2010-11-30 10:19:58 UTC
Permalink
I have a application where I use memory mapping to load the images. It
creates a temporary file using CreateFile() with CREATE_ALWAYS and
FILE_FLAG_DELETE_ON_CLOSE|FILE_ATTRIBUTE_TEMPORARY flags. Then it
creates a file mapping object using CreateFileMapping() and then maps
the memory using MapViewOfFile(). After mapping the view, it writes
some data into this region and unmaps the view of the file using
UnmapViewOfFile() (File mapping handle is not closed at this point and
will be closed at a later point of time). The problem is when I repeat
this process for about 7000 times the available RAM (checked using
perfmon) goes very low (I am running on 4GB RAM machine, available
physical memory goes to ~4MB). Because of this the system becomes
unusable. So it looks like OS is keeping the dirty pages in RAM only
even after unmapping. To solve this I tried using FlushViewOfFile(),
although it helps the performance of the system becomes very bad. For
example, if previouslt it took 50 seconds to map/unmap the 7000 images
with FlushViewOfFile it takes 100 seconds. Is there any way to solve
this problem (i.e. somehow remove my pages from RAM so that system
becomes responsive)? Also, does it make any difference if I run this
on Vista/Windows 7?
RossettoeCioccolato
2010-11-30 16:36:10 UTC
Permalink
How about the file handle used to create the file mapping: Do you close
that?
Naveen
2010-12-01 04:50:07 UTC
Permalink
On Nov 30, 9:36 pm, "RossettoeCioccolato"
How about the file handle used to create the file mapping:  Do you close
that?
Yes, I am closing the file handle returned using CreateFile() after
doing CreateFileMapping().
RossettoeCioccolato
2010-12-01 19:50:13 UTC
Permalink
Perhaps you should post some pseudo-code so that we can see what you are
doing. MapViewOfFile allocates some memory for the VAD descriptors
describing the mapped region. However, they are small. I don't see how
7000 of them is going to exhaust your memory.
n***@gmail.com
2013-11-20 13:08:31 UTC
Permalink
Post by RossettoeCioccolato
Perhaps you should post some pseudo-code so that we can see what you are
doing. MapViewOfFile allocates some memory for the VAD descriptors
describing the mapped region. However, they are small. I don't see how
7000 of them is going to exhaust your memory.
I had the same problem - if I make many calls MapViewOfFile + FlushViewOfFile + UnmapViewOfFile physical memory runs out, process virtual memory also runs out an application crashed. Pseudocode is very simple:

CreateFile
CreateFileMapping 8Gb // your machine physical memory size here

for(i = 0; i < 8 * 1024; ++i)
{
MapViewOfFile 1Mb
// here some changes in memory for it need to be saved to hdd

Sleep 5000 // application not trying to eat all memory, it just working

FlushViewOfFile
UnmapViewOfFile
}

CloseHandle Map
CloseHandle File


So the question is: how to make Windows release unmapped pages?

Loading...