How to fix high memory usage in Linux

Updated on 7/1/2023 – If you have noticed that your computer is running slow, it could be due to some applications consuming a large amount of memory without you realizing it. It is also possible that some services are using more RAM than usual,  even though Linux has a built-in memory management system that can set aside otherwise unused memory for disk caching to improve system performance. In this article, we will discuss how to identify memory-intensive applications and how to free up and manage memory for better performance. To diagnose Ubuntu (or Linux) memory usage, there are various tools that can help identify resource-intensive apps.

Read: Best Partition Managers for Linux Users

The ‘top’ command

The ‘top’ command can display the processes that are eating up large amounts of memory. In order to see this in action, open up your terminal and type in:

top

Memory usage on linux

The top command provides a dynamic, real-time overview of your system’s current workload. It displays a summarized, interactive, column-based list of threads and processes handled by the Linux kernel. The top half of the display shows the processes’ statistics and resource usage, while the lower half shows the currently active list of processes.

Here is a quick explanation of some of the important columns displayed by the top tool :

PID: The unique process id of the corresponding task.

USER: User name of the the corresponding task owner.

PR: Priority of the the corresponding task.

NI: ‘Nice’ Value of the corresponding task. A Negative ‘nice’ value indicates a higher priority whereas a positive ‘nice’ implies a priority.

Read: How to check memory usage on Ubuntu 22.04

VIRT: Total virtual memory currently used by the corresponding task.

RES: Resident size (kb) is the non swapped physical memory the corresponding task has used

SHR: The amount of currently shared memory used by the corresponding task.

%CPU: The corresponding task’s share of the already elapsed CPU time since the last screen

update, expressed as a % of total CPU time.

TIME+: CPU Time to the hundredths of a second is the the same as ‘TIME’, but with more granularity though.

SHR: Represents the Shared Memory size (kb) used by the corresponding task.

%MEM: Memory usage of the corresponding task.

Remember to press q whenever you want to exit the current mode.

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

Specific user processes

In order to display processes for a specific user, just type in the command :

This will show the following screen for user net2_admin :

Ubuntu memory usage

Running processes highlighting

When you press the ‘z‘ key while you are on running the top command, you will see currently running processes in different color for ease of identification.

Processes absolute path

When you press ‘c‘ while running the top tool, you will see the absolute path of running processes as indicated in the snapshot below :

Read: Processes in Linux – Guide for beginners

Running processes termination

When you press the ‘k’ key, you will be able to enter the PID of a process you want to kill. In the snapshot below you can see the location where you could enter the PID you wish to terminate.

Usually, processes that are killed all of a sudden can often be caused by the system running out of memory. It is at that moment that the so called OOM killer (Out-of-memory) steps in. If a task is terminated in order to save memory, it will be logged into several log files which are stored at /var/log/ .

In order to search for ‘out of memory’ message alerts in the log files, you can run the command :

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

Example log marks of killed processes by OOM, would look similar to the entry below :

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

This shows that the process that was terminated was mysqld with pid 1163. At the time it was killed, this has an OOM score of 511. Note that, depending on your current Linux distro and system configuration, your log messages may look different.

Read: How to speed up Linux?

If a crucial process for your web application is terminated due to an “out of memory” situation, you have two options: disallow processes from overcommitting memory (as described in the section at the end of this article), reduce the amount of memory required by the process, or increase the amount of memory in your server configuration.

The ‘free’ tool

Another useful utility to diagnose Ubuntu ram usage is the ‘free’ tool. The ‘free’ command shows the total amount of used and free swap and physical memory in the system. The caches and buffers used by the kernel are also displayed.

Just type in the following in the terminal :

free -m

Ubuntu ram usage

If you want a more human-readable format, just run the command below :

free –human

Read: How to Check Disk Space in Ubuntu 22.04

Here is an explanation of the columns above :

total : The amount of total installed memory

used : The amount of used memory

free : The amount of unused memory

shared : The amount of memory used [mostly] by tmpfs

buffers : The amount of memory used by the kernel buffers

cache : The amount of memory used by slabs and the page cache

buff/cache : The sum of cache and buffers

available : An estimation of how much amount of memory is available in order to start new applications without a need for swapping.

The ‘htop’ command

Much like the top tool above, htop is an interactive easy to read process viewer. It displays per process memory usage as well as other various parameters.

linux memory usage check

We have written a detailed article where the htop is covered in more detail .

The ‘ps’ tool

In order to find out which applications are using the most amount of memory, you can use the command ‘ps; which is similar to ‘top’. Simply invoke it as follows:

ps aux

This will display the apps that are using the most memory . They are identifiable by their process IDs.

Read : Ubuntu/Debian monitoring tools guide for system administrators

Vmstat utility

The vmstat command is used to monitor the performance of the system since it yields information about processes, paging, memory, disk, block IO and CPU. The vmstat is also known as reporter of virtual memory statistics .

In your terminal run the following command :

vmstat

Linux memory monitoring

It is also possible to have the values shown in one single column, by using vmstat -s :

vmstat -s

vmstat Linux

To display the units in megabytes, you should use the -sS M option.

SAR

SAR ( System Activity Report ) collects and reports information about CPU performance, Disk usage, Network monitoring, battery performance , Memory I/O devices consumption and many more. Generated information is provided on the fly and can output it in a file as well using the -o filename switch.

To use SAR, you would have to install it using the command:

sudo apt install sysstat

As soon as this is done, you would have to allow data collection. This is achieved by marking ENABLED=”true” in /etc/default/sysstat in Ubuntu for example.

Read: How to Configure Network Settings in Ubuntu 22.04

Fixing high memory usage

One of the common applications that can cause high memory usage on Linux is Java. If you have been using the Java runtime environment, it might give rise to some unexpected issues. If this has been running on a server, JBoss or Tomcat can be the culprits here. Their configuration files can be found in the following :

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

Other suspects can be MySQL or Apache. You can check the log files for potential warnings or even errors from these very services.

For example to look up an ‘out of memory’ error, you can use the grep command to search the timestamp at which this problem first occurred in the log files of your servers like Apache,Tomcat, MySQL etc.

For an http server for instance, you can also use the grep tool to investigate the access logs in order to check if there are any suspicious or abusive access to the server which might have caused a resource exhaustion attack :

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

The suspicious activities which can be identified from the access logs can be  :

  1. High access from one single IP addresses
  2. High access to resources or files which are unavailable.
  3. High incoming number of requests such as HTTP POST.
  4. High number of login requests or failed access attempts.

Read: How to use grep command in Linux

Memory Overcommit

Linux usually affords additional memory for a given process than what it actually requires. In Linux, overcommitting means giving out virtual memory to processes with no guarantee that the RAM or the physical storage for it exists. It is possible to manually configure the way Linux handles the overcommit feature.

There are two parameters involved in this overcommit configuration :

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

a – /proc/sys/vm/overcommit_memory

Three different settings are available here:

0: (this is the default) The Linux kernel can overcommit memory freely at any time, an algorithm checks if enough memory is available.

1: Memory overcommit will always be done by the Linux kernel. There is no on memory availability. Though it improves memory intensive workloads, it increases the risk of ‘out of memory’ situations.

2: Memory overcommit will not be carried out by the Linux kernel. Memory allocation is configured by overcommit_ratio.

Make sure you are logged in as a superuser before executing the command below :

echo 2 > /proc/sys/vm/overcommit_memory

b – /proc/sys/vm/overcommit_ratio

This setting is only accounted for when overcommit_memory = 2. This indicates how much physical RAM is used. Swap space goes on top of that. The default value is “50” or 50%..

As superuser run the command below :

echo 75 > /proc/sys/vm/overcommit_ratio

No Space Left on Device Linux error

1 – This could be related to iNodes which store information about files. Since many filesystems have a fixed number of inodes, it could be that the maximum number of iNodes has been reached (run out of inodes instead of disk blocks). To check that, use df command:

sudo df -i /

if no more inodes are available, you just need to delete unnecessary or obsolete files.

2 – You can also check if you have enough space left using :

df -h
or
df -i 

3 – Try to run Disk Usage Analyzer application so that you can find out what is eating up space.

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

Conclusion

We have discussed several tools that can be used to diagnose high memory usage in Linux and improve system performance. Unnecessary background applications that consume a lot of RAM, SWAP, or CPU power can cause the system to run slower or be less responsive. Identifying these processes, which may have started at boot, can help the user determine whether to adjust them or stop them entirely to free up more RAM and increase the use of SWAP..  

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

This Post Has 6 Comments

  1. Imraan

    Great post

  2. Mukundan Madurantakam

    Great post. Thanks

  3. alberto

    Very informative post. Thanks and happy new year!

  4. Don Johnson

    The best and most comprehensive article I’ve ever found. Thank you.

Leave a Reply