2

I have a situation where I may need to execute an SAP command line tool hdbsql when memory gets too low (to help clean out the HANA table cache).

How I can best approach this? I did have an idea to extract the free memory value from the top command (or get free/max*100 to get a %, which is better) in a shell script scheduled using crontab, but I cannot find anything on a possible approach on this anywhere, so I cannot even start anything.

3

You could use awk to calculate the percentage and the test utillity to determine if the value exceeds 90%, for example. The cronjob could look like this:

/usr/bin/test 90 -le $(/usr/bin/awk '$1=="MemTotal:"{t=$2} $1=="MemFree:"{f=$2} END{printf "%d", (t-f)/(t/100)}' /proc/meminfo) && command-to-cleanup

The awk part extracts the needed values out of /proc/meminfo, and then calculates the percentage of used memory. The test utillity checks if 90 is is less or equal (-le) to the calculated value. The part after the && would then be your tool that cleans up the memory (.. && command-to-cleanup). That cronjob can run every minute, for example:

* * * * * root /usr/bin/test 90 -le $(/usr/bin/awk '$1=="MemTotal:"{t=$2} $1=="MemFree:"{f=$2} END{printf "%d", (t-f)/(t/100)}' /proc/meminfo) && command-to-cleanup
2

IMHO the percentage of the free memory on a linux system is NOT a good indicator in this situation because of the way linux memory management works (memory is also used for buffering and caching, there's also swapping, etc) - plenty of info out there on the subject. Which means using such indicator would cause cleaning the cache earlier than actually necessary which I imagine comes with a price in overall performance.

I found that the percentage of swap used is usually a much better indication of the system being short on memory while taking all these into account: as long as it can still allocate memory it tipically won't use the swap.

As a bonus this doubles as a performance indicator as well - a system using over 4G of swap for example is usually barely crawling (think how much it takes to copy a 4G disk file from one place to another).

The swap info be found in /proc/meminfo, so the approach that chaos suggested can be adapted accordingly:

SwapTotal:      16777212 kB
SwapFree:       16777212 kB

You may need to experiment a bit to find out which swap usage percentage value is better fit for your particular case.

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.