Monitor Top Memory Processes
How to Monitor Top Memory Processes
Are you experiencing high memory usage and wondering which processes are consuming the most RAM in production? Need to monitor top memory-consuming processes continuously and receive alerts when processes exceed thresholds? This recipe focuses on configuring alert-driven monitoring of top memory processes, tracking memory usage over time, detecting memory leaks, and optimizing memory allocation on your Linux server with Zuzia.app.
Why Monitoring Top Memory Processes Matters
Memory-intensive processes can cause high RAM usage, leading to swap usage, performance degradation, and out-of-memory errors that can crash applications. Identifying which processes consume the most memory helps you detect memory leaks early, optimize memory allocation, troubleshoot performance issues, and plan capacity upgrades. Regular monitoring of top memory processes is essential for maintaining optimal server performance and preventing memory exhaustion.
Method 1: Find Top Memory Processes with ps Command
The ps command provides detailed process information and can be sorted by memory usage to identify the most memory-intensive processes.
Top 10 Memory Processes
To see the top 10 processes by memory usage:
# Top 10 memory processes with memory%, CPU%, and command
ps -eo %mem,%cpu,cmd --sort=-%mem | head -n 10
This shows:
- Memory percentage
- CPU percentage
- Full command
- Sorted by memory usage (highest first)
Top Memory Processes with PID
To include process ID:
# Top memory processes with PID
ps -eo pid,%mem,%cpu,cmd --sort=-%mem | head -n 10
This includes process ID, useful for process management.
Top Memory Processes with User
To see which user owns the processes:
# Top memory processes with user
ps aux --sort=-%mem | head -n 10
This shows user, PID, memory%, CPU%, and command.
Method 2: Find Top Memory Processes with top Command
The top command provides real-time process monitoring sorted by memory usage.
One-Time Memory Snapshot
To get a one-time snapshot sorted by memory:
# Memory snapshot (press 'M' to sort by memory in interactive mode)
top -b -n 1 -o %MEM | head -20
This shows system summary and top processes by memory usage.
Interactive Top Monitoring
For interactive monitoring:
# Interactive top (press 'M' to sort by memory)
top
Interactive top allows real-time monitoring and dynamic sorting by memory.
Method 3: Find Top Memory Processes with htop Command
If htop is installed, it provides a more user-friendly interface:
# Interactive htop (press F6 to sort by memory)
htop
# Install htop if needed
# Debian/Ubuntu: sudo apt-get install htop
# CentOS/RHEL: sudo yum install htop
htop provides color-coded memory usage and easier navigation.
Method 4: Automated Top Memory Process Monitoring with Zuzia.app
Manual checks work while you are looking. To keep running them without you, Zuzia uses its agent and scheduled tasks.
How it works in Zuzia
- Install the Zuzia agent on the server with one command. CPU, RAM, disk, load and network metrics start reporting on their own, with alerts on a threshold, on drift from the server's normal, or on a disk fill-up forecast.
- Save the commands from this guide as a scheduled task and choose how often it runs, from every minute to once a year. The agent runs it on every host you pick.
- If a command fails, you get an alert right away by email, Slack, Discord, push (through your Pushover account) or SMS (through your Vonage account).
- The output of every run is kept, so you can look back at what changed. On paid plans, AI can read the output and alert you only when it spots a problem.
Setting it up
- Add the server in the Zuzia dashboard and install the agent.
- Create a scheduled task with the check commands below, then choose the hosts and how often it runs.
- Choose your notification channels. Turn on maintenance mode during planned work so it doesn't trigger alerts.
Method 5: Advanced Memory Process Monitoring Techniques
Filter Processes by User
To see top memory processes for a specific user:
# Top memory processes for specific user
ps -u username -o %mem,%cpu,cmd --sort=-%mem | head -n 10
This helps identify which users are consuming the most memory.
Monitor Specific Processes
To monitor specific processes by name:
# Monitor specific process
ps aux | grep nginx | grep -v grep | sort -rk4,4 | head -n 5
# Monitor multiple processes
ps aux | grep -E "nginx|mysql|php" | grep -v grep | sort -rk4,4
This helps track memory usage for specific applications.
Alert on High Memory Usage
Create alert conditions:
# Alert if top process uses > 20% memory
TOP_MEM=$(ps -eo %mem --sort=-%mem | head -n 2 | tail -n 1 | cut -d. -f1)
if [ "$TOP_MEM" -gt 20 ]; then
echo "ALERT: Process using more than 20% memory"
fi
This helps detect high memory usage automatically.
Real-World Use Cases for Top Memory Process Monitoring
Memory Leak Detection
Detect memory leaks in applications:
# Top memory processes
ps -eo %mem,%cpu,cmd --sort=-%mem | head -n 10
# Track memory growth over time
ps -eo pid,%mem,cmd --sort=-%mem | head -n 5 > /tmp/mem-$(date +%Y%m%d-%H%M).txt
Set up Zuzia.app to check top memory processes every 5 minutes and track memory growth to detect leaks.
Application Performance Monitoring
Monitor application memory usage:
# Monitor web server processes
ps aux | grep -E "nginx|apache" | grep -v grep | sort -rk4,4
# Monitor database processes
ps aux | grep -E "mysql|postgres" | grep -v grep | sort -rk4,4
Track memory usage for critical applications to ensure optimal performance.
Capacity Planning
Use historical memory process data to plan capacity:
# Track peak memory processes
ps -eo %mem,%cpu,cmd --sort=-%mem | head -n 5 > /tmp/top-mem-$(date +%Y%m%d-%H%M).txt
Zuzia.app's historical data helps you understand peak memory requirements and plan server capacity accordingly.
Best Practices for Top Memory Process Monitoring
1. Monitor Regularly
Check top memory processes every 5-10 minutes for active monitoring. Less critical systems can be checked every 15-30 minutes. Use Zuzia.app to automate these checks.
2. Set Appropriate Thresholds
Set different alert thresholds:
- Warning: Process > 10% memory
- Critical: Process > 20% memory
- Emergency: Process > 30% memory
3. Track Memory Usage Trends
Use Zuzia.app's historical data to identify patterns. Understanding when processes consume memory helps detect memory leaks and plan optimization.
4. Compare with Total Memory
Monitor top memory processes alongside total memory usage. High memory usage with low total memory might indicate memory leaks.
5. Investigate Persistent High Memory Usage
If processes consistently use high memory:
- Check for memory leaks: Monitor memory growth over time
- Review application logs for errors
- Consider process optimization or restart
- Plan memory upgrades if needed
Troubleshooting Common Memory Process Issues
Process Using Excessive Memory
If a process uses excessive memory:
- Identify the process:
ps aux --sort=-%mem | head -n 1 - Check process details:
ps -p PID -o %mem,%cpu,cmd - Review application logs
- Check for memory leaks: Monitor memory growth
- Consider restarting if memory leak detected
Memory Leaks
If memory usage keeps growing:
- Monitor memory growth: Track process memory over time
- Identify leaking processes: Compare memory usage across time periods
- Review application code for memory leaks
- Restart processes if necessary
- Implement memory limits if possible
Out of Memory Errors
If out-of-memory errors occur:
- Check top memory processes:
ps aux --sort=-%mem | head -n 10 - Review system logs:
dmesg | grep -i "out of memory" - Check swap usage:
free -h - Optimize or restart memory-intensive processes
- Consider memory upgrade
FAQ: Common Questions About Monitoring Top Memory Processes
How often should I check top memory processes on Linux?
For production servers, check top memory processes every 5-10 minutes for active monitoring. Use Zuzia.app automated monitoring to check continuously without manual intervention. Less critical systems can be checked every 15-30 minutes.
What should I do if a process uses too much memory?
If a process uses excessive memory, investigate by monitoring memory growth over time, reviewing application logs for errors, verifying if high memory usage is normal for the application, and optimizing the process or planning memory upgrades. Use Zuzia.app to monitor and receive alerts when thresholds are exceeded so you can react quickly.
Can I filter top memory processes by user?
Yes, you can filter by user: ps -u username -o %mem,%cpu,cmd --sort=-%mem. This helps identify which users are consuming the most memory resources, useful for multi-user systems or troubleshooting user-specific memory issues.
How can I detect memory leaks?
To detect memory leaks, monitor memory usage over time: track process memory growth using Zuzia.app historical data, compare memory usage across time periods, identify processes with steadily increasing memory, and investigate processes that do not release memory after completing tasks.
How can I see memory usage trends over time?
Zuzia.app stores all memory process data historically in its database, allowing you to view memory usage trends over time. You can see historical data showing which processes consumed the most memory on different dates, identify patterns in memory usage, detect memory leaks, and plan optimizations based on trends.
What's the difference between %mem and RSS for memory usage?
%mem shows memory percentage relative to total system memory, while RSS (Resident Set Size) shows actual memory in kilobytes. %mem is useful for quick comparisons, while RSS provides precise memory values. Both are useful for monitoring, but %mem is more intuitive for percentage-based thresholds.
Can I monitor top memory processes across multiple Linux servers?
You can add multiple servers in Zuzia.app and monitor top memory processes across all of them simultaneously. Each server executes memory check commands independently, and all results are stored in Zuzia.app's database for centralized monitoring and comparison.
Does Zuzia.app use AI to analyze memory process patterns?
On paid plans, AI can read the output of your scheduled tasks and commands, explain it in plain language and alert you only when it spots a problem. Metric alerts work without AI: Zuzia alerts when CPU, RAM or load drifts from the server's normal and forecasts when a disk will fill up.
Related guides, recipes, and problems
- Guides:
- Recipes:
- Problems: