How to monitor memory usage in Linux

How to Monitor Memory Usage in Linux Memory monitoring is one of the most critical aspects of Linux system administration. Understanding how your system utilizes memory resources helps optimize performance, prevent crashes, and ensure smooth operation of applications and services. This comprehensive guide will teach you various methods to monitor memory usage in Linux, from basic command-line tools to advanced monitoring techniques. Whether you're a system administrator managing production servers or a developer optimizing applications, mastering memory monitoring tools is essential for maintaining healthy Linux systems. This article covers everything from simple one-line commands to sophisticated monitoring solutions that provide detailed insights into memory consumption patterns. Prerequisites and Requirements Before diving into memory monitoring techniques, ensure you have: - Access to a Linux system (any distribution) - Basic familiarity with the command line interface - Root or sudo privileges for certain advanced monitoring tasks - Understanding of basic Linux concepts like processes and system resources Understanding Linux Memory Management Linux memory management involves several key concepts: - Physical RAM: The actual hardware memory installed in your system - Virtual Memory: The combination of physical RAM and swap space - Swap Space: Disk space used as overflow when RAM is full - Buffer/Cache: Memory used by the kernel for disk I/O optimization - Shared Memory: Memory segments shared between processes Basic Memory Monitoring Commands Using the `free` Command The `free` command is the most fundamental tool for checking memory usage in Linux. It displays information about total, used, and available memory. ```bash free ``` This basic command shows output in kilobytes. For more readable output, use: ```bash free -h ``` The `-h` flag displays values in human-readable format (KB, MB, GB). Here's what each column means: - total: Total installed RAM - used: Currently used memory - free: Completely unused memory - shared: Memory used by tmpfs filesystems - buff/cache: Memory used for buffers and cache - available: Estimation of memory available for starting new applications Advanced `free` Command Options ```bash Display memory usage every 2 seconds free -h -s 2 Show memory statistics 5 times with 3-second intervals free -h -c 5 -s 3 Display wide format with additional details free -w -h ``` Monitoring with `top` Command The `top` command provides real-time system monitoring, including detailed memory information for individual processes. ```bash top ``` Key memory-related information in `top`: - VIRT: Virtual memory used by a process - RES: Physical memory currently used by a process - SHR: Shared memory used by a process - %MEM: Percentage of total RAM used by a process Customizing `top` for Memory Monitoring ```bash Sort processes by memory usage top -o %MEM Show only specific user processes top -u username Display memory in different units top -E m # Display in MB top -E g # Display in GB ``` Using `htop` for Enhanced Monitoring `htop` is an improved version of `top` with a more user-friendly interface and additional features. ```bash Install htop (if not already installed) sudo apt-get install htop # Debian/Ubuntu sudo yum install htop # CentOS/RHEL sudo dnf install htop # Fedora Run htop htop ``` `htop` advantages: - Color-coded output for better readability - Mouse support for navigation - Tree view of processes - Easy sorting and filtering options - Built-in process management capabilities Advanced Memory Monitoring Tools Using `vmstat` for System Statistics The `vmstat` command reports virtual memory statistics, providing insights into system performance. ```bash Display current memory statistics vmstat Monitor memory usage every 2 seconds, 10 times vmstat 2 10 Display statistics in MB vmstat -S M ``` Key `vmstat` memory columns: - swpd: Virtual memory used - free: Idle memory - buff: Memory used as buffers - cache: Memory used as cache Monitoring with `/proc/meminfo` The `/proc/meminfo` file contains detailed memory information that many monitoring tools use as their data source. ```bash Display all memory information cat /proc/meminfo Display specific memory metrics grep -E "MemTotal|MemFree|MemAvailable|Buffers|Cached" /proc/meminfo Monitor memory changes in real-time watch -n 1 'cat /proc/meminfo | head -20' ``` Using `smem` for Detailed Memory Analysis `smem` provides advanced memory reporting with proportional set size (PSS) calculations, giving more accurate memory usage per process. ```bash Install smem sudo apt-get install smem # Debian/Ubuntu sudo yum install smem # CentOS/RHEL Display memory usage by process smem Show memory usage by user smem -u Display memory usage with percentages smem -p Generate memory usage charts smem --pie=name -c "pss" ``` Process-Specific Memory Monitoring Using `pmap` Command The `pmap` command shows memory mapping of specific processes. ```bash Show memory map for a specific process ID pmap 1234 Display extended format with additional details pmap -x 1234 Show memory map with device information pmap -d 1234 ``` Analyzing Memory with `ps` Command The `ps` command can display memory usage information for running processes. ```bash Show processes sorted by memory usage ps aux --sort=-%mem | head -10 Display specific memory columns ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -20 Show memory usage for specific process ps -p 1234 -o pid,vsz,rss,comm ``` Using `/proc/[pid]/status` for Process Details Each process has detailed memory information in its `/proc/[pid]/status` file. ```bash Show memory details for process ID 1234 cat /proc/1234/status | grep -E "Vm|Rss" Monitor memory usage of a specific process watch -n 1 'cat /proc/1234/status | grep -E "VmSize|VmRSS|VmSwap"' ``` Monitoring Swap Usage Checking Swap Status ```bash Display swap usage free -h | grep Swap Show detailed swap information cat /proc/swaps Display swap usage with swapon swapon --show ``` Monitoring Swap Activity ```bash Monitor swap activity with vmstat vmstat 1 5 Check which processes are using swap for file in /proc/*/status; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file 2>/dev/null done | sort -k 2 -n | tail -10 ``` Automated Memory Monitoring Scripts Basic Memory Monitoring Script Create a simple script to log memory usage: ```bash #!/bin/bash memory_monitor.sh LOG_FILE="/var/log/memory_usage.log" while true; do TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') MEMORY_INFO=$(free -m | grep "Mem:" | awk '{print "Total: "$2"MB Used: "$3"MB Free: "$4"MB"}') echo "[$TIMESTAMP] $MEMORY_INFO" >> $LOG_FILE sleep 60 done ``` Advanced Memory Alert Script ```bash #!/bin/bash memory_alert.sh THRESHOLD=80 EMAIL="admin@example.com" MEMORY_USAGE=$(free | grep Mem | awk '{printf("%.2f"), $3/$2 * 100.0}') MEMORY_USAGE_INT=${MEMORY_USAGE%.*} if [ "$MEMORY_USAGE_INT" -gt "$THRESHOLD" ]; then echo "High memory usage detected: ${MEMORY_USAGE}%" | \ mail -s "Memory Alert - $(hostname)" $EMAIL # Log top memory-consuming processes echo "Top 10 memory consumers:" >> /var/log/memory_alert.log ps aux --sort=-%mem | head -11 >> /var/log/memory_alert.log fi ``` Graphical Memory Monitoring Tools System Monitor (GNOME) For desktop environments, graphical tools provide visual memory monitoring: ```bash Launch GNOME System Monitor gnome-system-monitor Install if not available sudo apt-get install gnome-system-monitor ``` Using `glances` for Web-Based Monitoring `glances` provides a web interface for system monitoring: ```bash Install glances pip install glances Run with web interface glances -w Access via browser at http://localhost:61208 ``` Memory Monitoring Best Practices Regular Monitoring Schedule 1. Real-time Monitoring: Use `top`, `htop`, or `glances` for immediate issues 2. Periodic Checks: Run `free -h` regularly during system maintenance 3. Automated Monitoring: Implement scripts for continuous monitoring 4. Historical Analysis: Log memory usage data for trend analysis Setting Up Memory Thresholds ```bash Create memory threshold monitoring #!/bin/bash WARN_THRESHOLD=75 CRIT_THRESHOLD=90 MEM_USAGE=$(free | grep Mem | awk '{printf("%.0f"), $3/$2 * 100}') if [ $MEM_USAGE -ge $CRIT_THRESHOLD ]; then echo "CRITICAL: Memory usage at ${MEM_USAGE}%" # Take corrective action elif [ $MEM_USAGE -ge $WARN_THRESHOLD ]; then echo "WARNING: Memory usage at ${MEM_USAGE}%" # Send notification fi ``` Memory Optimization Tips 1. Identify Memory Leaks: Monitor processes with consistently growing memory usage 2. Optimize Cache Usage: Understand that cached memory is available when needed 3. Configure Swap Properly: Ensure adequate swap space for your workload 4. Regular Cleanup: Remove unnecessary processes and services Troubleshooting Common Memory Issues High Memory Usage Investigation When experiencing high memory usage: ```bash Step 1: Identify top memory consumers ps aux --sort=-%mem | head -20 Step 2: Check for memory leaks Monitor specific process over time watch -n 5 'ps -p PID -o pid,vsz,rss,comm' Step 3: Analyze system caches echo 1 > /proc/sys/vm/drop_caches # Clear page cache (use with caution) ``` Out of Memory (OOM) Issues ```bash Check OOM killer logs dmesg | grep -i "killed process" Monitor OOM killer activity journalctl -f | grep -i oom Check system limits ulimit -a ``` Swap Issues ```bash Check if swap is being used excessively vmstat 1 10 | awk 'NR>2 {si+=$7; so+=$8} END {print "Swap in: "si" Swap out: "so}' Identify processes using swap grep -r VmSwap /proc/*/status 2>/dev/null | awk '$2 > 0 {print $1 $2}' | sort -k2 -n ``` Enterprise Memory Monitoring Solutions Using Nagios for Memory Monitoring ```bash Sample Nagios memory check command define command{ command_name check_memory command_line $USER1$/check_memory.pl -w 80 -c 90 } ``` Zabbix Memory Monitoring Zabbix provides comprehensive memory monitoring with: - Real-time memory usage graphs - Configurable alerts and thresholds - Historical data analysis - Custom memory monitoring templates Prometheus and Grafana For modern monitoring stacks: ```bash Install node_exporter for system metrics wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz tar xvfz node_exporter-.-amd64.tar.gz cd node_exporter-.-amd64 ./node_exporter ``` Memory Performance Tuning Kernel Memory Parameters Key kernel parameters for memory management: ```bash View current memory parameters sysctl -a | grep vm Important parameters to monitor: vm.swappiness - Controls swap usage tendency vm.dirty_ratio - Percentage of memory for dirty pages vm.vfs_cache_pressure - Controls cache reclaim pressure ``` Application-Specific Monitoring ```bash Monitor Java application memory jstat -gc PID 1s Monitor database memory usage For MySQL SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool%'; For PostgreSQL SELECT * FROM pg_stat_bgwriter; ``` Conclusion Effective memory monitoring in Linux requires understanding various tools and techniques, from basic commands like `free` and `top` to advanced monitoring solutions. Regular monitoring helps prevent performance issues, identify memory leaks, and optimize system resources. Key takeaways for successful memory monitoring: 1. Use Multiple Tools: Combine different monitoring tools for comprehensive coverage 2. Establish Baselines: Understand normal memory usage patterns for your systems 3. Automate Monitoring: Implement scripts and tools for continuous monitoring 4. Set Appropriate Thresholds: Configure alerts based on your system requirements 5. Regular Analysis: Review memory usage trends to identify potential issues early By implementing these memory monitoring practices, you'll maintain healthier Linux systems, prevent memory-related performance issues, and ensure optimal resource utilization. Remember that effective monitoring is an ongoing process that requires regular attention and adjustment based on changing system requirements and workloads. Continue expanding your Linux system administration skills by exploring related topics such as CPU monitoring, disk I/O analysis, and network performance monitoring to build comprehensive system observability capabilities.