Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a "cache managar" application which is capable of saving buffers to disk when they are consuming to much memory. By that fact, I think it is very "stupid" to use the page-file, as I want everything or to be in real-memory, or I myself will be saving buffers to disk. But, unfortunatelly, I am not able to disable the page file, except if I do that for the entire machine.

So, is it possible to disable the pagefile for a single application? In my case, it is better to receive an out-of-memory exception than to save anything in the page-file for such an application.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 9:29am    
Interesting question, Paulo. I wonder why? Mission-critical stuff? You probably know Windows will not guarantee any certain response time in any case, anyway...
--SA
E.F. Nijboer 18-Jan-11 10:48am    
You need to post any addition questions/comments as comment to that question instead of adding it as an answer because now no one is notified.
Paulo Zemek 18-Jan-11 14:40pm    
Now I understood the "post a comment instead of a new answer".
In fact, I have many purposes for this, but my biggest one is the session/viewstate manager. All view-state "buffers" (items in it, after serialization) are stored in memory, up to a number X of bytes. This is to make things faster. If more buffers than the "reserved memory" are needed, then they are saved to disk.

So, what I want is:
I have a computer with 16gb. I allow up to 5gb to be kept in memory. More than that is stored in disk (considering that old [less used] buffers are written first).
But, in fact, what I have is:
Even if only 100mb are allocated, the memory start to get written to the pagefile. So, the more buffers I keep in memory (to avoid disk accesses) the more disk accesses I have, as the pagefile get used more and more. And, even worst, the pagefile is used in a "random" order considering my needs, when it still has more than 10gb of memory free.

So, disabling the pagefile solves the problem. But, then, if for some reason another process someday needs that extra-memory, I will not have it.

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?
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 9:00am    
5+ Good point
Sergey Alexandrovich Kryukov 18-Jan-11 9:22am    
All right, My 5.
Joezer BH 21-Feb-13 3:14am    
Good to know! 5+
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 9:26am    
The idea is useful (my 5), but it's hard to say if it is applicable
Espen Harlinn 18-Jan-11 9:37am    
That's why I presented it as "a thought" :)
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!
 
Share this answer
 
v2
Comments
Espen Harlinn 18-Jan-11 10:06am    
This a good explanation about what I meant by "guard" pages :)
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.
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 9:59am    
Then you probably don't really have a problem with leaving the pagefile enabled, but need some other way to detect when you are using too much memory. If you want something exception based, you can use the "native" api and a technique called "guard" pages, but I suspect that this isn't in your best interest. Maybe using WeakReference objects to reference your cached elements would be a workable solution. That's about the best I can do based on the available information.
Dave Kreskowiak 18-Jan-11 14:25pm    
Your view seems to be very centric to what your app needs. You keep saying that you don't want it to use a pagefile, but what if Windows needs more physical memory for other applications and MUST swap out your app to do it? The pagefile isn't really there to give your app all kinds of memory backed by disk. It's there to give every app the resources it needs to run.
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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 10:08am    
Then I would need one heck of a lot more info on what you are doing to say anything sensible about it :)
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?
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 10:26am    
All things are possible, like getting mono http://www.mono-project.com/Main_Page, rewrite memory management to pin everything in memory using the approach outlined by E.F. Nijboer. So I can't say it's impossible - it probably wouldn't be a worthwhile exercise though. Sorry, off the top of my head, I can't come up with anything else ...
Espen Harlinn 18-Jan-11 10:28am    
Apart from using GCHandle http://dotnet.dzone.com/news/net-memory-control-use-gchandl, that is
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.
 
Share this answer
 
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?
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 10:59am    
Maybe something like this: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/541.mspx?mfr=true

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900