No, you can't disable the paging system for an application.
You may actually make things worse: you do realize that disk accesses are cached as well?
OriginalGriff has a good point, and it sounds like you possibly could benefit from using the
MemoryMappedFile Class[
^] to provide the required functionality. It's just a thought :)
Regards
Espen Harlinn
You can however lock it into physical memory using VirtualLock. I have not tried to lock the program (code section) but you could give it a try. Be aware that you must first get the right to write to that memory using VirtualProtectEx because normally that would have the PAGE_NOACCESS attribute. You also need to be aware that maybe some virus scanners may pick this up as self-modifying-code and report this as a threat.
http://msdn.microsoft.com/en-us/library/aa366895%28v=VS.85%29.aspx[
^]
http://msdn.microsoft.com/en-us/library/aa366899%28v=vs.85%29.aspx[
^]
For data memory you can simply use VirtualAllocEx and VirtualLock/VirtualUnlock. That should work very nicely.
Good luck!
Updated 18-Jan-11 4:02am
v2
Using a memory mapped file is the exact opposite to what I want.
I want to keep things in memory only, or to read them back from disk. As I know where the files are, using the pagefile is redundant.
I can already solve the problem disabling the pagefile for the entire system but, then, if another software needs the pagefile, it is not there. In fact what I know is that my application will never need the pagefile.
As a general rule, I understand that. I simple prefer avoiding to explain the application in every detail as I know that disabling the pagefile solves the problem. It already works even if I disable it for the entire system, but I want to avoid that if, for some reason, the system needs to use it. Actually, it is not needing the pagefile and, if I can limit the maximum memory the app could use (the real memory, not the managed memory, as I already force some collections if the application uses more memory than it should) everything will be OK.
In fact, what I want to say is:
My program can use up to X mb (for example 512mb). Trying to allocate even a single byte over that can throw an OutOfMemoryException. But, those X mb may never be swapped to disc, as they are already in disc.
I can create something to monitor overall system memory and, then, use less memory than the 512 (as this is the maximum), but the only thing I can't do at the moment is to avoid the pagefile. How I will avoid such application to kill the entire machine is, in fact, one of my challenges (I can make it use only 100mb, or I can make it use more memory, but I need to monitor the entire system), but that's not the problem if I can never avoid the data to be stored in the pagefile. That's my only concern at the moment, as all other problems are manageable.
As for the weak-references, they are already in use... but .Net keeps some memory allocated even if I do full collections... in fact .Net sees the memory as available, when windows is saving everything to disk.
Using VirtualLock is not a possibility, as I am trying to keep .Net objects in real memory, I don't have their real addresses, and also they can be moved around.
The problem is that windows uses the pagefile when it does not need to do that.
If my program allocates only 15mb, after 5 hours without any use, it is put into the pagefile. Then, simple to use it, the pagefile is read.
If I disable the pagefile, that does not happen. In fact, I know that my application does not need to be paged, but I want to keep the pagefile active for other softwares.
I only want to know: Is there any way to tell windows that, for a given program, its allocated memory must always be kept in real RAM?
No problem. I know this one is really hard.
The "nearest" solution to my problem is setting the MinWorkingSet to the maximum memory I allow my program to use. That's not a "nice" solution as the program will always consume its maximum memory, but I think it will solve my problem of having "already saved files" being saved to the pagefile.
So, another question: Is it possible to limit the maximum memory a process can allocate?
If I use Process.MaxWorkingSet I can tell how much RAM the process can use, but it is still allowed to allocate more memory (using the pagefile).
Is there a way to avoid this, causing OutOfMemoryExceptions to be thrown?
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)