1

This is essentially the reverse of "Linux: how to explicitly unswap everything possible?".

I want to maximize the amount of available free memory before running a process I know will use system memory intensively and I don't want it to pause for long periods of time until the OS gets it into its head that everything else should be swapped out.

Also, I know a lot of programs have memory they only use on initialization and then never touch again.

How do I make this happen?

I have tried doing sysctl vm.swappiness=100 but that hardly swaps out anything.

Sven
  • 100,763
Omnifarious
  • 400
  • 1
  • 3
  • 15

3 Answers3

2

The unused initialization code will be freed as soon as the memory is needed for other purposes. (It will be backed by files from which it is read.)

The memory paging mechanisms on Linux are well designed and have been tested for years. It is rare you would want to swap any process out of memory. This would result in heavy paging activity any time the swapped process is scheduled for execution.

If you truly need the memory from the other applications, you have too little memory. You can prevent the other programs from executing by sending them a STOP signal with the kill command. Be careful which programs you stop or you could lock yourself out of the system.

If you are experiencing large pauses during startup of your process, consider using sar to determine where the bottleneck is. You can also use top to determine which process are being paged or swapped heavily. Don't be surprised if your process shows up as the problem.

I've run servers which were severely starved for memory. To perform startups, it was essential to limit the number of processes starting at any one time. Process start almost instantaneously even if memory is far over-committed.

If you really want to force everything possible out of memory you could write a program that allocates the desired amount of memory and continually writes to each page of the allocated memory for a few loops. It will experience all the issues you want to avoid.

BillThor
  • 28,293
  • 3
  • 39
  • 70
1

You may be able to achieve what you're trying to accomplish by dropping the caches (pagecache, dentries and inodes).

echo 3 > /proc/sys/vm/drop_caches

will clear them all.

More information can be found at: Linux Memory Management - Drop Caches

0

Something like this:

cat > eatmyram.c <<EOF
void main() {
  while (1) {
    int* ip = malloc(1024*1024);
    // force the allocated memory to be dirty
    for (int i = 0; i < 256*1024; i++) ip[i] = i;
  }
}
EOF
gcc eatmyram.c -o eatmyram && ./eatmyram

This will allocate as much memory as it can and then die. Be warned that if you have slow swap, this will make your system unusable. Keep an eye on your free memory and a hand on Ctrl-C!


Now, let me explain why I just needed this. I use earlyoom to kill runaway compiler calls to keep them from making my system unusable, and zram to keep unused memory to a compact size. But if I have to reenable the zram device for some reason, Linux will take a while to realize that it should swap things out - and I can't force it to swap things out by just running compiler runs because 1. earlyoom will kill the compiler, and 2. if I turn earlyoom off it'll only free as much space as it needs for that one run. Turn earlyoom back on, and the next slightly bigger compiler run will die again. So instead, I just turn earlyoom off, use eatmyram.c to force all unused memory into zswap, turn earlyoom back on, and go on with my day.