How to fix high memory usage in Linux

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

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.

top

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
💡 Tip: Press ‘q’ to exit top command mode.

Specific User Processes

To display processes for a specific user:

top -u username

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:

sudo grep -i -r ‘out of memory’ /var/log/

🔧 The ‘free’ Tool

The ‘free’ command shows total amount of used and free swap and physical memory in the system.

free -m

For a more human-readable format:

free –human

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.

htop

🔍 The ‘ps’ Tool

To find applications using the most memory:

ps aux

📈 Vmstat Utility

Monitor system performance including processes, paging, memory, disk, block I/O, and CPU:

vmstat

For values in a single column:

vmstat -s

Display units in megabytes:

vmstat -s -S M

📊 SAR (System Activity Report)

SAR collects and reports information about CPU performance, disk usage, network monitoring, and memory consumption.

To install SAR:

sudo apt install sysstat
📝 Note: After installation, enable data collection by setting ENABLED=”true” in /etc/default/sysstat

🔧 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:

grep -ir “21/Aug/2019:11:02” /usr/local/apache/domlogs/

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
echo 2 > /proc/sys/vm/overcommit_memory

/proc/sys/vm/overcommit_ratio

Used when overcommit_memory = 2. Indicates physical RAM usage percentage (default: 50%):

echo 75 > /proc/sys/vm/overcommit_ratio

💽 “No Space Left on Device” Error

1. Check iNodes

This could be related to iNodes (store file information). Check if you’ve run out:

sudo df -i /

2. Check Available Space

df -h

Or check inodes:

df -i

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.

 

Leave a Reply