How to fix high memory usage in Ubuntu

If you’ve observed your computer’s performance degrading, becoming noticeably slower, the reason might be applications silently consuming excessive memory.

It’s also conceivable that certain system services are utilizing more Random Access Memory (RAM) than expected,

even though Linux incorporates an integrated memory management system designed to optimize performance by allocating unused memory for disk caching. This article aims to guide you through identifying applications that rely heavily on memory and provide methods for freeing up and efficiently managing memory to enhance overall system responsiveness. To effectively diagnose memory usage on Ubuntu or any Linux distribution, a range of tools are available to pinpoint resource-intensive applications.

Read: Best Partition Managers for Linux Users

The `top` Command

The `top` command is capable of displaying processes that are using a significant amount of memory. To see this in action, launch your terminal and enter:

top

Memory usage on linux

Memory usage on linux

The `top` command delivers a dynamic, real-time perspective of your system’s current operational load. It presents an interactive, column-organized summary list of threads and processes managed by the Linux kernel. The upper section of the display provides statistical data and resource consumption metrics for processes, while the lower section lists currently active processes.

Here’s a concise explanation of some key columns presented by the `top` utility:

PID: The unique Process ID assigned to each task.

USER: The username associated with the owner of the task.

PR: The scheduling priority of the task.

NI: The ‘Nice’ value of the task. A negative ‘nice’ value indicates a higher processing priority, whereas a positive value denotes a lower priority.

Read: Monitor and Optimize Memory Usage on Ubuntu 22.04 for Peak Performance

VIRT: The total virtual memory currently in use by the task.

RES: Resident Size (KB), representing the non-swapped physical memory utilized by the task.

SHR: The quantity of shared memory presently being used by the task.

%CPU: The task’s proportion of CPU time elapsed since the last screen update, expressed as a percentage of total CPU time.

TIME+: CPU time in hundredths of a second, offering finer granularity than the standard ‘TIME’ column.

SHR: Shared Memory Size (KB) employed by the task.

%MEM: The percentage of physical memory being used by the task.

Remember that pressing ‘q’ will exit the `top` interface.

Read: How to fix system program problem detected error on Ubuntu

Specific User Processes

To display processes specifically for a given user, simply enter the command:

top -u username command

This command will present the display for the user ‘net2_admin’, as shown here:

top command output for specific user

Ubuntu memory usage for a specific user

Running Processes Highlighting

By pressing the ‘z’ key while `top` is running, you can enable color highlighting to visually distinguish currently running processes, making them easier to identify.

top command with highlighted running processes

Processes Absolute Path

Pressing ‘c’ while `top` is active will toggle the display to show the absolute path of each running process, as illustrated below:

top command showing absolute process paths

Read: Processes in Linux – Guide for beginners

Running Processes Termination

When you press the ‘k’ key, you gain the ability to terminate a process by entering its PID. The image below highlights the input area where you can specify the PID of the process you wish to terminate.

top command process termination prompt

Sudden process terminations are often a consequence of the system exhausting its available memory. In such scenarios, the OOM (Out-of-Memory) killer intervenes. If a process is terminated to reclaim memory, this event is logged in several system log files located in the `/var/log/` directory.

To search for ‘out of memory’ alert messages within these log files, you can use the command:

sudo grep -i -r 'out of memory' /var/log/

Example log entries indicating OOM-killed processes will resemble the following:

kernel: Out of memory: Kill process 1163 (mysqld) score 511 or sacrifice child

This log indicates that the ‘mysqld’ process with PID 1163 was terminated. At the time of termination, it had an OOM score of 511. Note that the appearance of your log messages might vary based on your Linux distribution and system configuration.

Read: How to speed up Linux?

If a critical process for your web application is terminated due to an “out of memory” condition, you have several courses of action: prevent processes from overcommitting memory (as detailed later in this article), decrease the memory footprint of the process, or augment the memory capacity of your server.

The `free` Utility

Another valuable tool for analyzing Ubuntu RAM usage is the `free` utility. The `free` command provides a summary of total, used, and free physical and swap memory on the system. It also includes information about kernel buffers and caches.

Simply enter the following command in your terminal:

 

free -m

 

free -m command output

Ubuntu RAM usage with free -m

For a more user-friendly, human-readable output, execute the command below:

free --human

free --human command output

Read: How to Check Disk Space in Ubuntu 22.04

Here is a description of the columns displayed:

total: The total amount of RAM installed on the system.

used: The amount of RAM currently in use.

free: The amount of RAM that is currently free and unused.

shared: The amount of memory primarily used by tmpfs filesystems.

buffers: The memory allocated to kernel buffers.

cache: Memory used by the page cache and slab caches.

buff/cache: The combined sum of cache and buffers.

available: An estimate of the memory available for launching new applications without resorting to swap space.

The `htop` Command

Similar to `top`, `htop` is an interactive and easily navigable process viewer. It provides per-process memory usage details along with numerous other system parameters.

htop command output

Linux memory usage check with htop

We have prepared a detailed article that explores `htop` in greater depth.

The `ps` Tool

To determine which applications are the most memory-intensive, you can utilize the `ps` command, which is akin to `top`. Execute it with the following syntax:

ps aux

ps aux command output

This command will list applications ordered by memory usage, identifiable by their Process IDs.

Read : Ubuntu/Debian monitoring tools guide for system administrators

Vmstat Utility

The `vmstat` command is employed to monitor system performance, providing insights into processes, paging, memory, disk activity, block I/O, and CPU utilization. `vmstat` is also known as a virtual memory statistics reporter.

Run the following command in your terminal:

vmstat

vmstat command output

Linux memory monitoring with vmstat

To display values in a single column, use `vmstat -s`:

vmstat -s

vmstat -s command output

vmstat Linux single column output

To present units in megabytes, use the `-sS M` option.

vmstat -sS M command output

SAR

SAR (System Activity Report) gathers and reports comprehensive system information including CPU performance, disk usage, network activity, battery status, memory and I/O device utilization, and more. It provides real-time information and can save output to a file using the `-o filename` switch.

To utilize SAR, installation is required using the command:

sudo apt install sysstat

After installation, data collection must be enabled, typically by setting `ENABLED=”true”` in `/etc/default/sysstat` on Ubuntu systems.

Read: How to Configure Network Settings in Ubuntu 22.04

Fixing High Memory Usage

Java is a common application that can lead to high memory consumption on Linux. If you are using the Java runtime environment, it could potentially introduce unexpected memory issues. On servers, JBoss or Tomcat are often the culprits. Their configuration files are typically located here:

/usr/local/jboss/bin/run.conf
/usr/local/tomcat/bin/setenv.sh

Other potential sources of high memory usage include MySQL and Apache. Reviewing the log files for these services may reveal warnings or errors related to memory.

To investigate ‘out of memory’ errors, you can employ the `grep` command to search for timestamps indicating when these errors first occurred in the log files of services like Apache, Tomcat, or MySQL.

For an HTTP server, `grep` can also be used to analyze access logs to identify suspicious or excessive access patterns that might indicate a resource exhaustion attack:

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

Suspicious activities identifiable in access logs can include:

  1. Excessive access attempts originating from a single IP address.
  2. Frequent requests for resources or files that do not exist.
  3. A high volume of incoming requests, particularly HTTP POST requests.
  4. Numerous login attempts or repeated failed access attempts.

Read: How to use grep command in Linux

Memory Overcommit

Linux commonly allocates more memory to a process than it immediately requires. Memory overcommitting in Linux refers to granting virtual memory to processes without ensuring that physical RAM or swap space is actually available. You can manually adjust how Linux manages memory overcommitment.

Two parameters govern this overcommit behavior:

  • /proc/sys/vm/overcommit_memory
  • /proc/sys/vm/overcommit_ratio

a – /proc/sys/vm/overcommit_memory

This setting offers three configuration options:

0: (Default) The Linux kernel can freely overcommit memory, relying on an algorithm to check memory availability dynamically.

1: The Linux kernel will always allow memory overcommit, disregarding memory availability checks. While potentially benefiting memory-intensive workloads, it elevates the risk of ‘out of memory’ situations.

2: The Linux kernel will not permit memory overcommit. Memory allocation is limited by the `overcommit_ratio` setting.

Ensure you have superuser privileges before executing the command below:

echo 2 > /proc/sys/vm/overcommit_memory

b – /proc/sys/vm/overcommit_ratio

This setting is only relevant when `overcommit_memory` is set to 2. It dictates the percentage of physical RAM that can be used for memory allocation, with swap space considered additional. The default `overcommit_ratio` is “50,” or 50%.

As a superuser, run the following command:

echo 75 > /proc/sys/vm/overcommit_ratio

No Space Left on Device Linux Error

1 – This error can be related to iNodes, which store metadata about files. Filesystems have a limited number of inodes, and exceeding this limit can lead to “No Space Left on Device” errors even if disk space remains. To check inode usage, use the `df` command:

sudo df -i /

If inodes are exhausted, removing unnecessary files is required.

2 – You can also verify available disk space using:

df -h

or

df -i

3 – Consider using a Disk Usage Analyzer application to identify space-consuming files and directories.

Read: Securing Ubuntu: Best Practices for Keeping Your System Safe

Conclusion

We have explored several tools for diagnosing high memory usage in Linux and enhancing system performance. Unnecessary background applications consuming excessive RAM, swap, or CPU resources can degrade system responsiveness. Identifying these processes, especially those starting at boot, allows users to decide whether to adjust or terminate them to free up RAM and optimize swap usage.

 


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