How to Check Memory Usage by User in Linux Print

  • linux, memory usage, server management, monitoring, system administration, troubleshooting, vps, performance
  • 3

To check memory usage by user in Linux, use the command ps aux combined with awk to sum memory consumption per user, or install the smem tool for detailed memory reporting. These commands help you identify which users or processes are consuming the most RAM on your server, which is essential for troubleshooting performance issues and preventing out-of-memory errors.

Why Monitor User Memory Usage

Understanding memory consumption on your Linux server is critical for maintaining optimal performance. When your system runs low on memory, it can lead to slow response times, application crashes, or even complete server outages. By tracking which users are consuming the most resources, you can quickly identify the source of memory problems and take corrective action.

This is especially important on shared hosting environments or VPS servers where multiple users or applications run simultaneously. Regular monitoring helps you prevent one user or process from monopolizing system resources.

Method 1: Check Memory Usage Per User with ps and awk

The most straightforward way to check memory usage by user is using a combination of standard Linux commands. This command loops through all users and calculates their total memory consumption:

for USER in $(ps haux | awk '{print $1}' | sort -u); do ps haux | awk -v user=$USER '$1 ~ user { sum += $4} END { print user, sum; }' ; done

This command works on any Linux distribution without requiring additional software. It displays each user and their percentage of total memory usage. The output shows user names followed by their cumulative memory percentage.

Method 2: Show Memory Usage as Percentage

For a more detailed view that shows memory usage as a percentage of total available RAM, use this enhanced command:

TOTAL=$(free | awk '/Mem:/ { print $2 }'); for USER in $(ps hux -U $USER | awk '{print $1}' | sort -u); do ps hux -U $USER | awk -v user=$USER -v total=$TOTAL '{ sum += $6 } END { printf "%s %.2f\n", user, sum / total * 100; }'; done

This method first captures the total system memory using the free command, then calculates each user's percentage of that total. The output displays the user name followed by a precise percentage value with two decimal places.

Method 3: Using smem for Advanced Memory Reporting

The smem tool provides the most user-friendly and detailed memory reporting. Unlike basic commands, smem distinguishes between different types of memory allocation, including USS (Unique Set Size), PSS (Proportional Set Size), and RSS (Resident Set Size).

Installing smem

On most Linux distributions, you can install smem using your package manager:

  • Ubuntu/Debian: sudo apt-get install smem
  • CentOS/RHEL: sudo yum install smem
  • Fedora: sudo dnf install smem

Basic smem Usage

To view memory usage by user with smem, simply run:

smem -u

This displays a table showing each user, their process count, swap usage, and various memory metrics. For more readable output with human-friendly units (KB, MB, GB), add the -k and -t flags:

smem -u -t -k

The -t flag adds a total row at the bottom, while -k formats memory values in kilobytes, megabytes, and gigabytes instead of raw bytes. This makes it much easier to interpret the results at a glance.

Understanding smem Output

The smem output includes several columns:

  • User: The username or system account
  • Count: Number of processes owned by the user
  • Swap: Amount of memory swapped to disk
  • USS: Unique Set Size (memory used exclusively by this user)
  • PSS: Proportional Set Size (shared memory divided proportionally)
  • RSS: Resident Set Size (total memory in RAM, including shared)

For troubleshooting purposes, PSS is often the most useful metric as it provides a realistic view of memory consumption by accounting for shared libraries.

Best Practices for Memory Management

Once you've identified which users are consuming the most memory, consider these steps:

  • Review running processes for the high-memory users using ps aux | grep username
  • Check for memory leaks in applications that show continuously growing memory usage
  • Optimize or restart services that are using excessive memory
  • Consider upgrading your hosting plan if legitimate usage consistently approaches memory limits
  • Set up monitoring alerts to notify you before memory exhaustion occurs

OBHost provides various hosting solutions with different memory allocations to suit your needs. Regular monitoring helps ensure you're on the right plan for your usage patterns.

Frequently Asked Questions

What's the difference between USS, PSS, and RSS in memory reporting?

USS (Unique Set Size) shows memory used exclusively by a process, PSS (Proportional Set Size) divides shared memory proportionally among processes, and RSS (Resident Set Size) shows total memory including fully counted shared libraries. PSS gives the most accurate picture of actual memory usage because it fairly distributes shared memory, while RSS can overestimate usage by counting shared libraries multiple times.

How can I identify which specific processes are using the most memory?

Use the command ps aux --sort=-%mem | head -n 10 to display the top 10 memory-consuming processes. This shows detailed information including the user, process ID, memory percentage, and command. You can adjust the number after head -n to see more or fewer processes.

What should I do if memory usage is consistently above 90%?

First, identify the users and processes consuming memory using the methods above. Then, determine if the usage is legitimate or caused by runaway processes or memory leaks. Restart problematic services, optimize applications, or consider upgrading to a hosting plan with more RAM if your usage is legitimate and consistent.

Can I limit memory usage for specific users on Linux?

Yes, you can set memory limits using cgroups (control groups) or by configuring limits in /etc/security/limits.conf. For cgroups, you can create memory constraints that prevent individual users from exceeding specified thresholds, which is particularly useful on multi-user systems to prevent one user from affecting others.

If you need assistance monitoring memory usage or optimizing your server performance, the OBHost support team is available 24/7 to help. Visit our contact page or email support@obhost.org for technical assistance.


Was this answer helpful?

« Back