nice, ionice are not enough

You cannot limit "pace" of consuming memory, but you can limit it's total memory usage via various different mechanisms.

1) security limits Limit memory usage for user running the process via /etc/security/limits.conf. This may not work in your case if you are running this process as the same user working on different stuff.

Example:

username hard as 1000000

2) Control Groups You can - with cgroups, create a group and also limit the memory usage. Just create cgroup, like this:

# cat >> /etc/cgconfig.conf << EOF
group memlimit {
    memory {
        memory.limit_in_bytes = 1073741824;
    }
}
EOF

# cat >> /etc/cgrules.conf <<EOF
username memory   memlimit/
EOF

Offcourse - in both cases, you have to develop your program so that it can recover from failing to allocate more memory.

In case it can't, you just need to add more memory to your system, so that you can avoid swapping. Once the swapping starts, its in the hands of kernel, and you can't - for example - lower the priority of kswapd, and even if you could - that doesn't guarantee some of the programs you use wouldn't still get swapped out, thus causing even more slower system response. Just don't go there.


While the next will not help you on memory swapping, it should help you on IO impact of your process.

It seems that you should explicit set the level as well.

ionice -c2 -n5 ./slowscript.sh

C2 alone might not be enough, depending on your kernel.

Qoute from the manpage (man ionice)

          Note that before kernel 2.6.26 a process that has not asked for an I/O priority formally uses "none" as scheduling class, but the I/O scheduler will  treat  such
          processes  as if it were in the best-effort class.  The priority within the best-effort class will be dynamically derived from the CPU nice level of the process:
          io_priority = (cpu_nice + 20) / 5.

          For kernels after 2.6.26 with the CFQ I/O scheduler, a process that has not asked for an I/O priority inherits its CPU scheduling class.   The  I/O  priority  is
          derived from the CPU nice level of the process (same as before kernel 2.6.26).

Basically: every newly started process will get C2 N4 , so when you want to have the IO reduced to as low as possible, either go on idle only (C3) or C2 N7.