Operations 6 min read

How to Show Per‑Process Memory Usage in MB/GB Using awk and ps

This guide explains three practical ways to list each Linux process's memory consumption, converting the RSS value from kilobytes to megabytes or gigabytes with ps and awk, and provides detailed command explanations and formatting options for clear output.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Show Per‑Process Memory Usage in MB/GB Using awk and ps

Method 1: Simple MB conversion

ps -eo pid,rss,cmd --sort=-rss | awk 'NR>1 {printf "%-10s %-10s %-50s
", $1, $2/1024 "MB", $3}'

Explanation: -e: show all processes. -o pid,rss,cmd: output PID, RSS (in KB), and command name. --sort=-rss: sort by RSS descending. NR>1: skip the header line. %-10s: left‑align a string in a 10‑character field. $2/1024 "MB": convert RSS from KB to MB and append the unit. $3: the command name.

To display values in gigabytes, divide by 1024 twice:

ps -eo pid,rss,cmd --sort=-rss | awk 'NR>1 {printf "%-10s %-10s %-50s
", $1, $2/1024/1024 "GB", $3}'

Method 2: Table with KB header

ps -eo pid,rss,cmd --sort=-rss | awk '
BEGIN { printf "%-10s %-10s %-50s
", "PID", "RSS(KB)", "CMD" }
NR>1 { printf "%-10s %-10dKB %-50s
", $1, $2, $3 }'

Key points: BEGIN { … } runs before any input is processed, printing the table header. printf formats each line; %-10dKB prints the RSS as an integer followed by the literal "KB". NR is awk's built‑in line counter; the condition NR>1 skips the header generated by ps. $1, $2, $3 correspond to PID, RSS, and command name respectively.

Method 3: Automatic unit conversion (KB/MB/GB/B)

ps aux | awk '
BEGIN {
    KB = 1024;
    MB = KB * 1024;
    GB = MB * 1024;
}
NR > 1 {
    mem = $6; # RSS column in KB
    if (mem > GB) {
        printf "%s (%s) - %.2f GB
", $11, $2, mem/GB;
    } else if (mem > MB) {
        printf "%s (%s) - %.2f MB
", $11, $2, mem/MB;
    } else if (mem > KB) {
        printf "%s (%s) - %.2f KB
", $11, $2, mem/KB;
    } else {
        printf "%s (%s) - %d B
", $11, $2, mem;
    }
}'

This script defines conversion constants, extracts the RSS field ($6), and prints the memory usage using the most appropriate unit, showing the command name ($11) and the owning user ($2).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShell scriptingMemory Usagepsawk
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.