Prevent heavy process immersion in the swap file

Our service, as a rule, falls asleep at night on our client server, and then hardly wakes up. It seems that a bunch of the process, which sometimes takes several hundred MB, is moved to the page file. This happens at night when our service is not used, and others are scheduled to be launched (database backups, AV scans, etc.). When this happens, after several hours of inactivity, the first call to the service takes up to several minutes (consecutive calls take seconds).

I am quite sure that this is a virtual memory management problem, and I really hate the idea of ​​forcing the OS to save our service in physical memory. I know that this will affect other processes on the server and reduce the overall server throughput. Having said that, our customers just want our app to be responsive. They don’t care if night work takes longer.

I vaguely remember how to make Windows store pages in physical memory, but I really hate this idea. I am more inclined towards some kind of internal or external watchdog timer that initiates functions of a higher level (there is already some kind of internal scheduler that does very little and does not make any difference). If there was a third-party tool providing such a service, it would be just as good.

I would like to hear any comments, recommendations and general solutions to this problem. The service is written in VC2005 and runs on Windows servers.

+6
performance windows virtual-memory
source share
5 answers

As you already mentioned, forcibly storing an application in memory is not the best way to share resources on a computer. A quick solution that you can find that works well is to simply schedule an event that wakes up your service at a specific time each morning before your customers start using it. You can simply schedule it in the Windows Task Scheduler with a simple script or EXE call.

+8
source share

I am not saying that you want to do this, or that this is best practice, but you may find that it works well enough for you. It seems to match what you requested.

Summary On each page in the process, on the page, click regularly.

How about a thread that runs in the background and wakes up every N seconds. Each time a page wakes up, it tries to read the address X. The attempt is protected by an exception handler if you read a bad address. Then increase X by page size.

There are 65536 pages in 4 GB, 49152 pages in 3 GB, 32768 pages in 2 GB. Divide your downtime (night dead time) by how often you want (try) to hit each page.

BYTE *ptr; ptr = NULL; while(TRUE) { __try { BYTE b; b = *ptr; } __except(EXCEPTION_EXECUTE_HANDLER) { // ignore, some pages won't be accessible } ptr += sizeofVMPage; Sleep(N * 1000); } 

You can get the sizeOfVMPage value from the dwPageSize value in the return result from GetSystemInfo ().

Do not try to avoid the exception handler using if (! IsBadReadPtr (ptr)), because other threads in the application can simultaneously change memory protection. If you peel off, because of this, it is almost impossible to determine why (most likely, this will be a disjoint state of the race), so do not waste time on it.

Of course, you want to disable this thread during the day and only start it during your dead time.

+2
source share

A third approach might be to have your service run a thread that does something trivial, like incrementing a counter and then sleeping for quite a while, say, 10 seconds. Thios should have minimal effect for other applications, but save at least some of your pages.

+1
source share

Another thing is that your data is localized.

In other words: do you really need all 300 megabytes of memory before you can do anything? Can the data structures used be rearranged so that any particular request can be satisfied with just a few megabytes?

for example

  • if 300 megabytes of heap memory contains face recognition data. Can the data be arranged so that face and woman data are stored together? Or are large noses separated from small noses?

  • if it has some kind of logical structure, can it be sorted? so can binary search be used to skip a large number of pages?

  • if it is a reliable database engine built into the database, can you better index / cluster the data so as not to require so many accesses to memory pages?

  • If they are textures of images, often used textures can be located next to each other?

Do you really need all 300 megabytes of memory before you can do anything? You cannot request a request without returning all this data to memory?


Otherwise: scheduled task at 6 ᴀᴍ to wake him up.

+1
source share

In terms of cost, the cheapest and easiest solution is probably just to buy more RAM for this server, and then you can completely disable the page file. If you are using 32-bit Windows, just buy 4 GB of RAM. Then the entire address space will be supported with physical memory, and the page file will do nothing.

0
source share

All Articles