How to Tell Linux Not to Swap Out a Particular Processes' Memory

Can I tell Linux not to swap out a particular processes' memory?

You can do this via the mlockall(2) system call under Linux; this will work for the whole process, but do read about the argument you need to pass.

Do you really need to pull the whole thing in-core? If it's a java app, you would presumably lock the whole JVM in-core. I don't know of a command-line method for doing this, but you could write a trivial program to call fork, call mlockall, then exec.

You might also look to see if one of the access pattern notifications in madvise(2) meets your needs. Advising the VM subsystem about a better paging strategy might work out better if it's applicable for you.

Note that a long time ago now under SunOS, there was a mechanism similar to madvise called vadvise(2).

Preventing a linux process being swapped out

For a single process, mlock and its relatives are the only way, I believe.

System-wide, you might find you get better performance by fiddling with the "swappiness". Try sysctl vm.swappiness to read the value and sysctl -w vm.swappiness=N to set it to N, where is some smaller number than it is currently. (Try 0 if you want to be extreme, telling the kernel that it should always evict cached pages in preference to swapping.)

Can I tell Windows not to swap out a particular processes’ memory?

You can use VirtualLock to prevent memory from being paged to disk, but I really think you're better off letting the OS manage the system's memory. It's pretty good at it, and I wouldn't second guess why the OS was swapping things to disk unless I really knew what I was doing.

How to find out which processes are using swap space in Linux?

Run top then press OpEnter. Now processes should be sorted by their swap usage.

Here is an update as my original answer does not provide an exact answer to the problem as pointed out in the comments. From the htop FAQ:

It is not possible to get the exact size of used swap space of a
process. Top fakes this information by making SWAP = VIRT - RES, but
that is not a good metric, because other stuff such as video memory
counts on VIRT as well (for example: top says my X process is using
81M of swap, but it also reports my system as a whole is using only 2M
of swap. Therefore, I will not add a similar Swap column to htop
because I don't know a reliable way to get this information (actually,
I don't think it's possible to get an exact number, because of shared
pages).

What are the exact conditions based on which Linux swaps process(s) memory from RAM to a swap file?

I've seen a lot of people posting subjective explanations of what this does. Here is hopefully a more full answer.

In the split LRU on post 2.6.28 Linux swappiness is a multiplier used to arbitrarily modify the fraction that is calculated determining the pressure built up in both LRUs.

So, for example on a system with no free memory left - the value of the existing memory you have is measured based off of the rate of how much memory is listed as 'Active' and the rate of how often pages are promoted to active after falling into the inactive list.

An LRU with many promotions/demotions of pages between active and inactive is in a lot of use.

Typically file backed storage is cheaper and safer to evict when your running out of memory and automatically is given a modifier of 200 (this makes file backed memory 200 times more worthless than swap backed memory (Which has a value of 0) when it multiplies this fraction.

What swappiness does is modify this value by deducting the swappiness number you gave (default 60) to file memory and adding the swappiness value you gave as a multiplier to anon memory. Thus the default swappiness leaves you with anonymous memory being 80 times more valuable than file memory (200-60 for file, 0+60 for anon). Thus, on a typical linux system that has used up all its memory, page cache would have to be 80 TIMES more active than anonymous memory for anonymous memory to be swapped out in favour of page cache.

If you set swappiness to 100 this gives anon a modifier of 100 and file memory a modifier of 100 (200 - 100) leaving both LRUs equally weighted. Thus on a file heavy system that wants page cache providing the anon memory is not as active as page cache then anon memory will be swapped to disk to make space for extra page cache.

Linux: How do I get the swap used by a process?

Check if you have VmSwap in /proc/$PROCESS/status. For example: grep VmSwap /proc/1/status

How can I tell Linux to keep a page and not evict it?

I think you are looking for mlock.

How can I know which process is using swap?

From here:

[a] /proc/meminfo - This file reports statistics about memory usage on
the system. It is used by free to report the amount of free and used
memory (both physical and swap) on the system as well as the shared
memory and buffers used by the kernel. You can also use free, vmstat
and other tools to find out the same information.

[b]
/proc/${PID}/smaps, /proc/${PID}/status, and /proc/${PID}/stat : Use
these files to find information about memory, pages and swap used by
each process using its PID.

[c] smem - This command (python script) reports memory usage with
shared memory divided proportionally.

Also you can refer Find out what is using your swap

#!/bin/bash
# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm --no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'`
do
let SUM=$SUM+$SWAP
done
echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
let OVERALL=$OVERALL+$SUM
SUM=0

done
echo "Overall swap used: $OVERALL"


Related Topics



Leave a reply



Submit