Search code examples
windowsmemory-managementmemory-mapped-files

Memory-mapped files remain in physical memory?


I have a process that uses a lot of memory mapped files.
Problem is that those files are kept in physical memory, even when the machine is low on memory, and other processes require this memory.

I've tried using SetProcessWorkingSetSize to limit the process working set, but it doesn't help, the process' working set keeps growing over the max value.

Is there a better way to limit the process' working set?
Can I change Windows' heuristcs for paging memory mapped files?


Solution

  • Ended up using brute force VirtualUnlock.

    PROCESS_MEMORY_COUNTERS pmc;
    if (GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
            if (pmc.WorkingSetSize > MaxWorkingSetSize)
            {
                    VirtualUnlock(FilePtr.pData, MaxWorkingSetSize);
                    UnmapViewOfFile(FilePtr.pData);
                    CloseHandle(FilePtr.hFileMap);
                    CloseHandle(FilePtr.hFile);
            }
    }