2

I don't won't to use swap file (due to some bug in kernel or AMD driver).

I wan't to have some util running and monitoring free system memory and alerting me when it becomes less than some specified limit.

This will notify me that I need to close some applications (or browser tabs) to avoid system freeze due to some strange kswapd0 I/O activity (probably another bug).

Is there any appropriate software?

UPDATE:

I've redesigned script provided by Gary for my need and want to share it

#!/bin/bash

#Minimum available memory limit, MB
THRESHOLD=400

#Check time interval, sec
INTERVAL=30

while :
do

    free=$(free -m|awk '/^Mem:/{print $4}')
    buffers=$(free -m|awk '/^Mem:/{print $6}')
    cached=$(free -m|awk '/^Mem:/{print $7}')
    available=$(free -m | awk '/^-\/+/{print $4}')

    message="Free $free""MB"", buffers $buffers""MB"", cached $cached""MB"", available $available""MB"""

    if [ $available -lt $THRESHOLD ]
        then
        notify-send "Memory is running out!" "$message"
    fi

    echo $message

    sleep $INTERVAL

done
1
  • 3
    It is little funny that my question is marked as a duplicate of another question answered with a copy of my answer I wrote in UPDATE of my question :-)
    – humkins
    Mar 5, 2016 at 15:05

2 Answers 2

6

You could try using free.

free -s n will update the output every n seconds. Wrap that in an if for whatever threshold you feel is "too much memory" being used, and display a message when it reaches that point.

EDIT: Here's the script I came up with. Rough, but it works.

#Checks for low memory.

#!/bin/bash

#cutoff_frac is basically how much used memory can be at in terms of how much
#total memory you have...2 is 1/2 of 100% or an alert when you're using 50% mem, etc.
cutoff_frac=2

total_mem=$(free|awk '/^Mem:/{print $2}')
let "threshold = $total_mem / $cutoff_frac"

while :
do

    free_mem=$(free|awk '/^Mem:/{print $4}')

    if [ $free_mem -lt $threshold ]
        then
        notify-send "Low memory!!"
    fi

    sleep 5

done

exit
5
  • This is not what I'm looking for. I need a kind of popup being bubbled so I don't have to keep my eyes on console.
    – humkins
    Mar 30, 2013 at 19:48
  • I'm trying to write one that does this - and I'll post it here if I do finish - but for reference, notify-send is just the command you're looking for - it'll make a popup.
    – Gary
    Mar 30, 2013 at 20:33
  • @gumkins the script has been edited into the answer
    – Gary
    Mar 31, 2013 at 3:03
  • Thank you Gary very much! "notify-send" is a command which certainly will help me in this. Also thank you for your script, I'll test it soon.
    – humkins
    Mar 31, 2013 at 7:52
  • 1
    Cool script, but isn't there any GUI tool to do this?
    – jave.web
    Mar 6, 2018 at 16:36
1

PHP version of the script:

The Free memory as shown in the system moniter is : Free= Total-(Used-buffered-cached)

What is the difference between the memory usage report in System Monitor and the one by free?

NOTE: To run this script as a cron job use:

* * * * *  env DISPLAY=:0.0 path/to/file 90 

or

@restart  env DISPLAY=:0.0 path/to/file

uncomment the while loop to run it manually:

Code:

#!/usr/bin/php
<?php
$alert_percent=($argc>1)?(int)$argv[1]:90;
//$interval=($argc>2):(int)$argv[2]:25;



//while(true)
//{
 exec("free",$free);

$free=implode(' ',$free);
preg_match_all("/(?<=\s)\d+/",$free,$match);

list($total_mem,$used_mem,$free_mem,$shared_mem,$buffered_mem,$cached_mem)=$match[0];

$used_mem-=($buffered_mem+$cached_mem);

$percent_used=(int)(($used_mem*100)/$total_mem);

if($percent_used>$alert_percent)
exec("notify-send 'Low Memory: $percent_used% used'");

//sleep($interval);
//}
exit();
?>
1
  • Getting issue #!/usr/bin/php Notice: Undefined variable: argc in /var/www/html/UsageMonitor/memoryusage.php on line 3 please advise Jan 4, 2017 at 13:08

Not the answer you're looking for? Browse other questions tagged .