In some circumstances, you may notice that your computer is running slow. This might be due to applications consuming a large chunk of memory without you noticing it.
Linux has an inherent memory management mechanism that can set aside otherwise free or unused memory for disk caching, which helps speed up your system. In this guide, we’ll explore how to identify memory-intensive applications and manage memory for better performance.
📊 The ‘top’ Command
The ‘top’ command displays processes consuming large amounts of memory and provides a real-time overview of your system’s workload.
Important Columns in Top Command
Column | Description |
---|---|
PID | The unique process ID of the corresponding task |
USER | Username of the corresponding task owner |
PR | Priority of the corresponding task |
NI | ‘Nice’ value of the task. Negative = higher priority, positive = lower priority |
VIRT | Total virtual memory currently used by the task |
RES | Resident size (kb) – non-swapped physical memory used |
SHR | Amount of shared memory used by the task |
%CPU | Task’s share of elapsed CPU time as percentage |
%MEM | Memory usage of the corresponding task |
TIME+ | CPU time to hundredths of a second |
Specific User Processes
To display processes for a specific user:
Interactive Top Commands
- ‘z’ key: Highlight running processes in different colors
- ‘c’ key: Show absolute path of running processes
- ‘k’ key: Kill a process by entering its PID
⚠️ OOM Killer: Processes killed suddenly may be due to the system running out of memory. The OOM (Out-of-Memory) killer terminates processes to save memory. Check logs with:
🔧 The ‘free’ Tool
The ‘free’ command shows total amount of used and free swap and physical memory in the system.
For a more human-readable format:
Free Command Columns Explained
Column | Description |
---|---|
total | Amount of total installed memory |
used | Amount of used memory |
free | Amount of unused memory |
shared | Memory used mostly by tmpfs |
buffers | Memory used by kernel buffers |
cache | Memory used by slabs and page cache |
buff/cache | Sum of cache and buffers |
available | Estimation of memory available for new applications |
⚡ The ‘htop’ Command
Similar to top, htop is an interactive, easy-to-read process viewer with enhanced features.
🔍 The ‘ps’ Tool
To find applications using the most memory:
📈 Vmstat Utility
Monitor system performance including processes, paging, memory, disk, block I/O, and CPU:
For values in a single column:
Display units in megabytes:
📊 SAR (System Activity Report)
SAR collects and reports information about CPU performance, disk usage, network monitoring, and memory consumption.
To install SAR:
🔧 Fixing High Memory Usage
Common Culprits
One common application causing high memory usage is Java. If you’re running Java runtime environment, JBoss, or Tomcat on a server, check these configuration files:
- /usr/local/jboss/bin/run.conf
- /usr/local/tomcat/bin/setenv.sh
Other suspects include MySQL or Apache. Check log files for warnings or errors:
Suspicious Activities to Look For
- High access from a single IP address
- High access to unavailable resources or files
- High number of incoming requests (HTTP POST)
- High number of login requests or failed access attempts
💾 Memory Overcommit
Linux typically allocates more virtual memory to processes than physically available. Overcommitting means giving out virtual memory with no guarantee that physical RAM exists.
Overcommit Parameters
/proc/sys/vm/overcommit_memory
Three settings available:
- 0: (default) Kernel overcommits freely, algorithm checks available memory
- 1: Always overcommit. Improves memory-intensive workloads but increases OOM risk
- 2: No overcommit. Memory allocation configured by overcommit_ratio
/proc/sys/vm/overcommit_ratio
Used when overcommit_memory = 2. Indicates physical RAM usage percentage (default: 50%):
💽 “No Space Left on Device” Error
1. Check iNodes
This could be related to iNodes (store file information). Check if you’ve run out:
2. Check Available Space
Or check inodes:
3. Use Disk Usage Analyzer
Run the Disk Usage Analyzer application to identify what’s consuming space.
✅ Best Practices
- Regularly monitor system resources using the tools mentioned
- Keep your system updated to benefit from memory optimizations
- Consider adding more RAM or optimizing applications if issues persist
- Configure swap space appropriately for your system’s needs
🎯 Conclusion
We’ve explored several tools to diagnose high memory usage in Linux and improve overall system performance. Identifying unnecessary background applications consuming RAM, SWAP, or CPU power helps decide whether to optimize or terminate them, freeing up memory resources for better system responsiveness.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.