I have some script that consumes a lot of swap memory.

How can I measure maximum swap load during script execution period ?

Is there any specific bash cli tool for my task ?

  • Well you can use some shell script that will check swap value in some interval of time and update the max value. – Prvt_Yadav Oct 31 '18 at 14:13
trapexit() {
    echo -e "\nmax swap usage is $MAXUSAGE kb"
    exit 0
trap trapexit SIGINT
while true; do
    CURUSAGE=`free |awk '/Swap/{print $3}'`;
    if [[ $CURUSAGE -gt $MAXUSAGE ]]; then

Well you can use shell script to find the maximum value of swap memory used.

#! /bin/bash
echo "$max" > max_value.txt
while true
#sleep 1
j=$(free -m | grep Swap| awk -F ' ' '{print $3}');
if [ "$max" -lt "$j" ]; then
echo "$max" > max_value.txt

Initially I set max=0 and wrote the output to some file max_value.txt.

j=$(free -m | grep Swap| awk -F ' ' '{print $3}');

This line will store the value of free swap memory available. And the if condition will compare this value to max value if it is greater then it will update the value of max and write it to max_value.txt file.

Now I am checking the free swap memory constantly so it can consume more CPU to run this script. However I have commented the line sleep 1 you can use it to reduce the CPU consumption(I checked on my laptop there was reduction of CPU consumption from 10% to 1%), but it may happen that you will not get accurate result. If the variation in used swap memory is not sudden then you can get an approximate result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.