How to control RAM / swap usage

18

4

Is there a way (application) to control RAM / swap usage policy at application level.

I have some applications that I use all the time and I want them to stay in RAM all the time and there are also application that I use quite seldom and they might be thrown to swap at demand. I know that default configured windows should give me best performance but I want to check whether I can get more :).

I use windows 7.

Darqer

Posted 2011-01-24T14:18:15.053

Reputation: 976

2+1. I am interested in a method that works for XP. – Xavierjazz – 2011-01-24T16:28:57.820

Answers

9

Yes there is: have the program call the VirtualLock function:

Pages that a process has locked remain in physical memory until the process unlocks them or terminates. These pages are guaranteed not to be written to the pagefile while they are locked.

I believe the SetProcessWorkingSetSize function might also be helpful.


For those people thinking I'm a heretic for even suggesting that this might be useful:

Notice that there are can be valid reasons for locking pages in memory. A valid reason might be that the user is running a CD burner, and the CD burner constantly pages out and hence causes a buffer underrun, creating a coaster. In that situation, It would be valid to lock the memory buffers so that they aren't paged out, even if that means the rest of the computer slows to a crawl, because that's still better than giving the user a coaster. There can be other valid reasons too -- my point is, while it's usually not recommended to lock pages in memory, valid situations still exist, and I don't think killing the idea would help the OP here.


Update: Take a look at Process Hacker's Reduce Working Set menu option.

user541686

Posted 2011-01-24T14:18:15.053

Reputation: 21 330

Perhaps quoting this bit from the article would put it in context: "Locking pages into memory may degrade the performance of the system by reducing the available RAM and forcing the system to swap out other critical pages to the paging file. Each version of Windows has a limit on the maximum number of pages a process can lock. This limit is intentionally small to avoid severe performance degradation." (My emphasis) – None – 2011-03-19T19:04:05.183

1@Randolph: I never recommended it, I just mentioned that, unlike what most other people seem to think, it's possible. I personally just hate telling the OP "Let's kill your idea" -- I think the OP's understood that the fact that he shouldn't do this, but if he still wants to, I think he has the right to know how to. (There's a reason they created these functions, and I'm not the person to judge whether the OP should use them or no.) – user541686 – 2011-03-19T19:05:00.507

There is a lot of code in Windows that was left in for backward compatibility. If there is a reason for having it, that would be it. One could compare it to the Shrink feature in SQL Server, which should not be used under any circumstances, but was left in for compatibility reasons. Windows 7 is a modern OS with proper memory management, and the page file should not be deleted, nor should programs try to stay in memory outside of the "rules". It's very poor practice and can result in an unstable and / or unusable system. – None – 2011-03-19T19:12:50.040

@Randolph: But my question is, why do you care if the OP's system is unstable? It's not like he's killing himself (unlike in your driving analogy), so why does that matter? – user541686 – 2011-03-19T19:14:09.213

If he uses VirtualLock (which as I've pointed out, is unlikely since I don't think he's a developer), he is killing all of the other applications that are being swapped out. The computer is not the car in my analogy. The computer is the road. – None – 2011-03-19T19:22:05.090

@Randolph: Not really, because the road affects other people, but his computer in no way affects you. So why does it matter to you? (Also, see my edit for an example of a valid reason.) – user541686 – 2011-03-19T19:23:30.580

How would I, as a normal Windows user, lock the memory buffers so that they aren't paged out, in your example? What is the process to do this? – None – 2011-03-19T19:25:24.757

1@Randolph: (I see you abandoned the analogy. :] ) Make a tiny program (shouldn't be more than ~10 lines) to open the other process and call SetProcessWorkingSetSize. That's one way. If course, if you don't know what that means because you're not a developer, then obviously there's not much you can do. But it's still possible, and saying it's impossible is still incorrect, it just requires more knowledge than the OP might have. – user541686 – 2011-03-19T19:27:34.647

I'm not abandoning my analogy, but as you feel it necessarily to change my point of view for me, I'm not engaging any further. Your -1 stands. – None – 2011-03-19T19:28:49.513

1@Randolph, you can download various freeware apps that force running apps to all go to the pagefile (by calling SetProcessWorkingSetSize in each process). – Chris O – 2011-03-19T19:29:30.273

@Randolph: Okay. By the way, did you read my reason about the CD burner? Any thoughts on that? Do you think it's a valid reason? (That's a decision I had to make myself, when I was writing a CD burner. Turned out I needed admin privileges to lock pages in memory -- and for good reason -- but it was still possible.) – user541686 – 2011-03-19T19:32:21.180

I have no opinion of it because I don't have all the facts in front of me. – None – 2011-03-19T19:55:43.563

@Randolph: Okay, just wondering. – user541686 – 2011-03-19T19:58:24.323

5

The only way I found is to disable the page file. However it's not a perfect solution: you will get error messages if you go near or over your RAM limit, and it may not save crash dumps properly, so turn it back on if you need to troubleshoot. Other than that, I have been using Win7 with no pagefile for months without a hitch.

My experience: I had this problem with a piano software (1GB RAM usage) which would be throw into swap after not being used for a while. Playing a piano key a few hours later would result in a glitchy mess since the swap would not be reloaded fast enough for the sound to play. (I would then put my whole arm on every key to force a reload!)

If RAM is a concern, I suggest you buy more (eg. 8 to 12GB with Windows 7 x64). For the RAM-conscious, having plently of RAM is a godsend.

But before, you should consider getting a SSD drive and putting your swap on it. It will accelerate swap recovery greatly, as well as providing many other system benefits. RAM does not provide benefits when not in use.

mtone

Posted 2011-01-24T14:18:15.053

Reputation: 11 230

3Do. Not. Delete. The. Page. File. Ever. – None – 2011-01-24T22:42:12.393

7I. Have. Been. Using. Win7. With. No. Pagefile. For. Months. Without. A. Hitch. – mtone – 2011-01-24T23:19:38.193

1I also use Win7 without page file all the time, however it seems to me that it might be not enough for my current work :( – Darqer – 2011-01-25T00:10:41.660

1@mtone, unfortunately, your "works on my machine" evidence does not apply to anyone else. Microsoft recommends using the page file, so I figure they know what they're talking about. Disabling the page file can easily result in low memory conditions. – None – 2011-01-25T00:23:52.213

1@Randolph Potter "your evidence does not apply to anyone else" - Wow, have a nice day. – mtone – 2011-01-26T16:47:49.863

3@Randolph: MS knows what they're talking about, because they know that their customers aren't exactly always computer pros. But for those people who actually know exactly what the pagefile does and doesn't do, there's no reason to force them into a practice that they don't need. – user541686 – 2011-03-19T04:47:01.377

@Mehrdad: best practice is called best practice for a reason. I can drive my car over the speed limit, without wearing a seatbelt, and I'll probably survive. But if I crash and hurt myself, I have no recourse. Using VirtualLock and deleting the page file is like driving over the speed limit and not wearing a seatbelt. We're here on this site to advocate good advice. – None – 2011-03-19T19:02:53.060

2@Randolph: If you drive over the speed limit without a seatbelt, you can die and also kill other people, not to mention increasing the insurance costs for others. The worst that can happen if your computer pages out its memory is that you'll just lose a bit of time, depending on the situation. Don't you see something a little (read: a lot) wrong with the analogy here? Also, have you used Linux/Unix? Ever noticed how they give you more than enough rope to hang yourself with, unlike Windows? People still use them -- is that like driving under influence or something? – user541686 – 2011-03-19T19:08:15.237

Consider the OP's question. He asked if there is a way to keep "some applications" in memory all the time. I have a strong suspicion that he is not necessarily a developer, so how would VirtualLock help? – None – 2011-03-19T19:17:39.553

2

Page file usage depends on a lot of factors. If you use a lot of programs and don't have enough RAM for all of them at the same time, then parts of them (or all) may be swapped out to the page file.

If you have 4GB of RAM and let's say around 3GB of it is available due to video cards and other resource users blocking out parts of the 4GB, and you only use a few programs that don't take a whole lot of memory, theoretically you should never need a swap file. However, Windows Vista and 7 are notoriously aggressive in their swapping of programs to disk.

I used to have a setting that reduced the swappiness of Windows 7 (swappiness is a Linux term but it applies to Windows just as well, and since there is a registry setting to tame Windows swapping aggressiveness it seems Microsoft agrees grudgingly, albeit in a well hidden manner), but I cannot seem to find it anymore. Searching for it is how I ended up here. The best alternative to this setting that I have found is to make sure that you use a fixed size swap file (I go for 4096-4096 on 32-bit OSes, and 12GB on 64-bit ones as I have 6GB of system RAM) on a dedicated partition. Having a spare 7200rpm 200GB drive, I place all of my swap partitions (windows XP, 7, 7 64-bit and various Linuxes) on this drive. It seems to work for me, but YMMV.

Paul

Posted 2011-01-24T14:18:15.053

Reputation: 21

1

Worth mentioning: when windows detects you're frequently using certain applications, it has the superfetch put those apps into memory so they'll load faster. I think trust windows to do its thing here.

CreeDorofl

Posted 2011-01-24T14:18:15.053

Reputation: 2 053