tags:

views:

123

answers:

3

I have plenty of RAM, however, after starting and finishing a large number of processes, it seems that most of the applications' virtual memory has been paged to disk, and switching to any of the older processes requires a very long time to load the memory back into RAM.

Is there a way, either via Windows API or via kernel call, to get Windows to unpage all (or as much as possible) memory? Maybe by stepping through the list of running processes and get the memory manager to unpage each process's memory?

A: 

No, windows provides no such feature natively. Programs such as Cacheman and RAM IDLE accomplish this by simply allocating a large chunk of RAM, forcing other things to page to disk, which effectively accomplishes what you want.

Billy ONeal
Erm, it sounds as if it would give the opposite of what he wants.
anon
@Neil: Does he want things paged in or paged out? It's difficult to tell from "unpaged" ... if he wants everything paged in than he is looking for Windows' default behavior, so I assumed he wanted non-default behavior, which would be paging everything out to disk.
Billy ONeal
Sorry if that wasn't clear. I want to get everything _into_ physical memory, _off_ the hard disk. Even though I have 2GB RAM, every time I Alt-Tab to another process, it appears that that process has to be retrieved from the disk first, which I want to avoid by making a single "put everything back into physical memory" call, having coffee, and then coming back to a fast and responsive system.
Hack59
@Hack59: Then you are looking for default behavior. Windows doesn't just decide randomly to page things to disk -- it does so only when it runs out of physical memory.
Billy ONeal
@Neil: No doubt, but what's the default behaviour for reverting the process? The point is that at some stage I _am_ running more processes than I have physical RAM for, so stuff does get paged to disk, but when most of those processes have finished, I appear to be left with lots of free physical RAM but all my older processes still paged to disk. [to be continued]
Hack59
[continued] Maybe I'm wrong about stuff being written to disk; could you recommend a way to check, e.g. using ProcessExplorer? I'm basically going on a subjective feeling; at the end of the day I have four open programs (Acrobat, Opera, Thunderbird, Putty), but switching between any of them requires like five seconds each just reading from disk.
Hack59
@Hack59. You can see the number of page faults per process in process explorer. I'd be surprised if that could explain what you are seeing on its own though unless your disc is about to die or something weird like Windows has stopped using DMA mode http://winhlp.com/node/10.
Martin Smith
@MSmith: I don't think it's any problem of that sort; after a fresh reboot everything is perfect. It's only after the machine has been up for a few days and I've run (and finished) lots of video encoding, playback and 3D games that my "constant" processes (browser, email) seem to have been paged to disk and not recovered after the memory becomes available again. Even right now, with about 600MB free RAM, Opera has 70M total page faults, with about 100 new ones per second. Can I find out how much of Opera's memory is on disk, and instruct the system to load it into RAM?
Hack59
Basically, in that language, is there a way to modify a running process so that it will never incur any page faults, provided I have enough free physical RAM and do not launch any further processes? Like telling the system, "don't swap to disk any more", without actually disabling swapping?
Hack59
@Hack59: No, there's no way to do that other than to disable the page file altogether.
Billy ONeal
+3  A: 

Well, it isn't hard to implement yourself. Use VirtualQueryEx() to discover the virtual addresses used by a process, ReadProcessMemory() to force the pages to get reloaded.

It isn't likely to going to make any difference at all, it will just be your program that takes forever to do its job. The common diagnostic for slow reloading of pages is a fragmented paging file. Common on Windows XP for example when the disk hasn't been defragged in a long time and it was allowed to fill close to capacity frequently. The SysInternals' PageDefrag utility can help fix the problem.

Hans Passant
Thank you for both those pointers! Let me check if using the two functions makes any difference on the performance of switching processes!
Hack59
Yes, because calling ReadProcessMemory on every byte of every process is surely going to perform better than just paging things back in..... Oh, BTW, you'll need Admin privies and take the SE_DEBUG_NAME privilege in order to use the process memory functions.
Billy ONeal
Oh, no, I don't actually want to read every byte, I just want to tell the memory manager that I might be about to do so... Surely the memory manager must know whether parts of a process's memory have been paged out; I'm surprised that it has no interface to request paging memory back for a specific process _without_ actually requesting content from that memory.Re "every byte of every process": I don't actually mind if it does all that in one go while I'm on coffee break, as long as when I come back everything is in memory.
Hack59
Just read a single byte per page.
Hans Passant
@HP: Good idea, thanks!
Hack59
A: 

OK, based on the replies so far, here's a naive suggestion for a tool that tries to get all applications back into physical memory:

  1. Allocate a small chunk of memory X, maybe 4MB. (Should it be non-pageable?)
  2. Iterate over all processes:
    • For each process, copy chunks of its memory to X. (Possibly suspending the process first?)

Suppose you have 2GB of RAM, and only 1GB is actually required by processes. If everything is in physical memory, you'd only copy 256 chunks, not the end of the world. At the end of the day, there's a good chance that all processes are now entirely in the physical memory.

Possible convenience and optimisation options:

  • Check first that the total required space is no more than, say, 50% of the total physical space.
  • Optionally only run on processes owned by the current user, or on a user-specified list.
  • Check first whether each chunk of memory is actually paged to disk or not.

I can iterate over all processes using EnumProcesses(); I'd be grateful for any suggestions how to copy an entire process's memory chunk-wise.


Update: Here is my sample function. It takes the process ID as its argument and copies one byte from each good page of the process. (The second argument is the maximal process memory size, obtainable via GetSystemInfo().)

void UnpageProcessByID(DWORD processID, LPVOID MaximumApplicationAddress, DWORD PageSize)
{
  MEMORY_BASIC_INFORMATION meminfo;
  LPVOID lpMem = NULL;

  // Get a handle to the process.
  HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);

  // Do the work
  if (NULL == hProcess )
  {
    fprintf(stderr, "Could not get process handle, skipping requested process ID %u.\n", processID);
  }
  else
  {
    SIZE_T        nbytes;
    unsigned char buf;

    while (lpMem < MaximumApplicationAddress)
    {
      unsigned int stepsize = PageSize;

      if (!VirtualQueryEx(hProcess, lpMem, &meminfo, sizeof(meminfo)))
      {
        fprintf(stderr, "Error during VirtualQueryEx(), skipping process ID (error code %u, PID %u).\n", GetLastError(), processID);
        break;
      }

      if (meminfo.RegionSize < stepsize) stepsize = meminfo.RegionSize;

      switch(meminfo.State)
      {
      case MEM_COMMIT:
        // This next line should be disabled in the final code
        fprintf(stderr, "Page at 0x%08X: Good, unpaging.\n", lpMem);

        if (0 == ReadProcessMemory(hProcess, lpMem, (LPVOID)&buf, 1, &nbytes))
          fprintf(stderr, "Failed to read one byte from 0x%X, error %u (%u bytes read).\n", lpMem, GetLastError(), nbytes);
        else
          // This next line should be disabled in the final code
          fprintf(stderr, "Read %u byte(s) successfully from 0x%X (byte was: 0x%X).\n", nbytes, lpMem, buf);

        break;
      case MEM_FREE:
        fprintf(stderr, "Page at 0x%08X: Free (unused), skipping.\n", lpMem);
        stepsize = meminfo.RegionSize;
        break;
      case MEM_RESERVE:
        fprintf(stderr, "Page at 0x%08X: Reserved, skipping.\n", lpMem);
        stepsize = meminfo.RegionSize;
        break;
      default:
        fprintf(stderr, "Page at 0x%08X: Unknown state, panic!\n", lpMem);
      }

      //lpMem = (LPVOID)((DWORD)meminfo.BaseAddress + (DWORD)meminfo.RegionSize);
      lpMem += stepsize;
    }
  }

  CloseHandle(hProcess);
}

Question: Does the region by whose size I increment consist of at most one page, or am I missing pages? Should I try to find out the page size as well and only increment by the minimum of region size and page size? Update 2: Page size is only 4kiB! I changed the above code to increment only in 4kiB steps. In the final code we'd get rid of the fprintf's inside the loop.

Hack59
After some testing, I'm finding that this does actually work pretty well! It certainly has the effect that an application thus "unpaged" becomes responsive again and doesn't require any unexpected hard disk access.
Hack59