Memcalc
Memcalc is a small GPL-licensed Bash tool that answers a simple question without reaching for heavy machinery: how much memory is actually being used by a service, a PID, or all processes owned by a user?
I use it in particular when several processes belong to the same service and a quick look at ps does not immediately provide the useful total.
Three calculation modes
– --service adds up the memory used by processes with the requested command name.
– --pid displays the usage of the specified PID or PIDs.
– --user adds up the usage of all processes owned by a user.
Several services, PIDs or users can be requested in one run. Memcalc collects the complete process inventory once, then performs every calculation against that coherent snapshot.
Usage
Display help or the version:
./memcalc.sh --help
./memcalc.sh --versionCalculate the memory used by several services:
./memcalc.sh --service apache2 php-fpm sshdCheck one or more PIDs:
./memcalc.sh --pid 1234 5678Calculate usage for several users:
./memcalc.sh --user www-data mysqlOutput is expressed in megabytes. When a target does not exist or currently uses no memory, the script says so explicitly instead of printing a misleading result.
What version 1.1 fixes
This version deliberately keeps the tool straightforward while making its execution more reliable:
– arguments remain proper Bash values, without accidental word splitting;
– strict PID validation;
– one complete ps collection;
– refusal to publish a partial report if collection or calculation fails;
– help, version information and support for multiple targets.
Dependencies and limits
Memcalc only uses Bash, ps and awk. It does not require root privileges, although an unprivileged user is naturally limited by the process visibility provided by the system.
The name supplied to --service must exactly match the command name returned by ps. This is not a cgroup analyser or a replacement for systemd-cgtop: it is a quick, readable calculation that is easy to automate.
Resources
Script: memcalc.sh
—
Christophe Casalegno (find all my links, projects and social networks at all.bo)