Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell Windows not to swap out a particular processes’ memory?

Tags:

.net

memory

swap

Is there a way to tell Windows that it shouldn't swap out a particular processes' memory to disk?

Its a .Net windows service with fairly large memory usage. I got lot of physical RAM but the OS seems to move part of the process memory to the pagefile anyway.

like image 624
Luca Martinetti Avatar asked Jun 24 '09 15:06

Luca Martinetti


People also ask

How do I not use swap memory?

To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back into RAM. It also means that you need to be sure you have the RAM to support this operation. An easy way to do this is to run 'free -m' to see what is being used in swap and in RAM.

Does Windows do swap memory?

Windows uses the swap file to improve performance. A computer normally uses primary memory, or RAM, to store information used for current operations, but the swap file serves as additional memory available to hold additional data.


2 Answers

You can use VirtualLock to prevent memory from being paged to disk, but I really think you're better off letting the OS manage the system's memory. It's pretty good at it, and I wouldn't second guess why the OS was swapping things to disk unless I really knew what I was doing.

like image 83
Eric Petroelje Avatar answered Oct 25 '22 19:10

Eric Petroelje


VirtualLock by default will run out of quota in a hurry, even if you permission the user (by default the security policy setting of "Lock pages in memory" has no entries (not even admin)).

If you do configure this, you will need to set "Adjust memory quotas for a process" also.

This question is a bit ambiguous, what part of the VM space do you want to prevent from being swapped out? Heap/Stack/Module's?...?

Overall, one solution that I had used in the past, is to create a DLL with a large section which was READ + WRITE, unmoveable, you can also mark it EXECUTE and SHARED if need be, but then I would HeapCreate into that module's section, allowing me to use it as a heap.

You can look up the exact bit's to set here, IMAGE_SCN_MEM_NOT_PAGED looks like it would do the trick.

If you go through the process of marking all of your module's and sections with this bit, the Windows module loader will not charge a articular user quota or require policy settings be modified on each system you deploy your code. You will need to code in a special shim to provide access to the HEAP's you create into these NON_PAGEABLE dll's also.

It is a bit of work but it worked well for me in the past, I had the added requirement to share memory to multiple processes, which was based at the same address.

I would think the easiest thing to try would be to disable your page file entirely. But before this, if your using Vista/2008+ OS's, the swapping you see may be due to superfetch, which could be proactively "tuning" your system with the assumption that in the near future you need to use one application or another. Some other easy tasks would be to stop unused services like search, which could be indexing huge amounts of files, also you should go into your "task scheduler", which configures system tasks, by default there are a few dozen on most systems with default action's that will transmit all Dr. Watson dump's to MS, defrag your drive and a number of other possibly exceedingly memory intensive type operations.

You could reply with a bit more detail to get some better answers... but one other suggestion would be to simply purchase a large solid state drive and use that exclusively for swap, beware that these degrade over time in overall performance an size due to bad block mappings that are common with all existing SSD technologies.

I recently came across a codeplex project called "non-paged clr host", from their page:

Implementation From an implementation perspective, the non-paged CLR host uses the SetProcessWorkingSetSize, SetProcessWorkingSetSizeEx (on Windows Server 2003 and above) and VirtualLock APIs to ensure that memory allocated by it is locked into physical memory. Note that using the above APIs does not guarantee with absolute certainty that no paging will occur; instead, it minimizes the odds of it occurring to very exceptional scenarios. In some load tests we have conducted, even when the system as a whole was hogged by lack of physical memory, no page faults were observed in the process using the non-paged CLR host.

like image 21
RandomNickName42 Avatar answered Oct 25 '22 20:10

RandomNickName42