Home » Linux » How to log the memory consumption on Linux?

How to log the memory consumption on Linux?

Posted by: admin November 30, 2017 Leave a comment

Questions:

Is there any ready-to-use solution to log the memory consumption from the start of the system? I’d like to log the data to simple text file or some database so I can analyze it later.

I’m working on Linux 2.4-based embedded system. I need to debug the problem related to memory consumption. My application automatically start on every system start. I need the way to get the data with timestamps from regular intervals (as often as possible), so I can track down problem.

The symptoms of my problem: when system starts it launched my main application and GUI to visualize the main parameters of the system. GUI based on GTK+ (X server). If I disable GUI and X server then my application works OK. If I enable GUI and X server it does not work when I have 256 MiB or 512 MiB of physical memory installed on the motherboard. If I have 1 GiB of memory installed then everything is OK.

Answers:

A small script like

rm memory.log
while true; do free >> memory.log; sleep 1; done

Questions:
Answers:

The following script prints time stamps and a header.

#!/bin/bash -e

echo "      date     time $(free -m | grep total | sed -E 's/^    (.*)//g')"
while true; do
    echo "$(date '+%Y-%m-%d %H:%M:%S') $(free -m | grep Mem: | sed 's/Mem://g')"
    sleep 1
done

The output looks like this (tested on Ubuntu 15.04, 64-bit).

      date     time          total       used       free     shared    buffers     cached
2015-08-01 13:57:27          24002      13283      10718        522        693       2308
2015-08-01 13:57:28          24002      13321      10680        522        693       2308
2015-08-01 13:57:29          24002      13355      10646        522        693       2308
2015-08-01 13:57:30          24002      13353      10648        522        693       2308

Questions:
Answers:

I think adding a crontab entry will be enough

*/5 *  *  *  *  free -m >> some_output_file

There are other tools like SeaLion, New Relic, Server Density etc which will almost do the same but are much easier to install and configure. My favorite is SeaLion, as it being free and also it gives a awesome timeline view of raw outputs of common linux commands.

Questions:
Answers:

There’s a program called

sar

on *nix systems. You could try to use that to monitor memory usage. It takes measurements at regular intervals. Do a

man sar

for more details. I think the option is -r for taking memory measurements, -i to specify the interval you’d like.

Questions:
Answers:

You could put something like

vmstat X >> mylogfile

into a startup script. Since your application is already in startup you could just add this line to the end of the initialization script your application is already using.
(where X is # of seconds between log messages)