Code Snippet

Just another Code Snippet site

[Swap] How-to

# Determine the size of the swap file in megabytes and multiply by 1024 to determine the number of blocks. 
# For example, the block size of a 64 MB swap file is 65536
dd if=/dev/zero of=./swapfile bs=1024 count=65536

# Setup the swap file
mkswap ./swapfile

# enable the swap file immediately
swapon ./swapfile

In /etc/fstab add :

/swapfile          swap            swap    defaults        0 0

,


4 thoughts on “[Swap] How-to

  • Olivier says:

    https://blog.sleeplessbeastie.eu/2016/12/26/how-to-display-processes-using-swap-space/

    Display processes using swap space

     find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") printf "%10s %-30s %20s\n",process["Pid"],process["Name"],process["VmSwap"]}' '{}' \;
    

    Display processes using swap space sorted by used space

    find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") printf "%10s %-30s %20s\n",process["Pid"],process["Name"],process["VmSwap"]}' '{}' \; | awk '{print $(NF-1),$0}' | sort -h | cut -d " " -f2-
    

    Display top ten processes using swap space with percentage values

     find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/swaps | sed 1d | awk 'BEGIN{sum=0} {sum=sum+$(NF-2)} END{print sum}')" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") {used_swap=process["VmSwap"];sub(/[ a-zA-Z]+/,"",used_swap);percent=(used_swap/TOTSWP*100); printf "%10s %-30s %20s %6.2f%\n",process["Pid"],process["Name"],process["VmSwap"],percent} }' '{}' \;  | awk '{print $(NF-2),$0}' | sort -hr | head | cut -d " " -f2-
    
  • Check swap (file and native) :

    cat /proc/swaps

Leave a Reply

Your email address will not be published. Required fields are marked *