How to Fix High Memory Usage in Linux

Linux Fix High Memory Usage Featured

Our computers have more memory than they’ve ever had. Chances are good that this is true no matter when you’re reading this. That said, no matter how much memory you have, it never seems to be quite enough.

You can pack your computer or server full of all the RAM it will take, and something will always find a use for it. Most of the time this is a good thing. After all, what is the point of all that RAM if you don’t use it? On the other hand, if you’re limited, running out of memory can be a problem.

Are You Sure About Your Memory Usage?

Before you start looking to free up memory, you want to make sure that something is actually using it. Because of the way Linux handles memory, it just might seem like something is using all of your available memory.

Linux Fix High Memory Usage Top

As Linux Ate My RAM explains, the top command showing that you’re low on memory may not mean what you think it means. By default, Linux sets aside otherwise unused memory for disk caching, which actually helps speed up your computer.

That said, there could be some problematic services or applications that are actually using more RAM than they should be.

Diagnosing Memory Usage

To diagnose your memory, you’ll need to use a few different commands. The top command is good for looking at any applications that may be eating up too much RAM – just don’t rely solely on the PhysMem stat showing at the top.

You can use the free and ps commands to help diagnose any RAM issues, too. To use free, just run the following:

You’ll notice that the “total” and “used” figures will be similar. As we mentioned above, this is normal behavior for Linux, so don’t worry about this. The most important thing to pay attention to is the “used” section in the “buffers/cached” row, as this is what applications and services are actually using.

Linux Fix High Memory Usage Free

You can use ps similarly to top to determine which applications are using the most memory. To this, simply run the following:

This will show you which apps are using the most memory and show you their process IDs.

Linux Fix High Memory Usage Ps

Fixing Your High Memory Usage

There are a few common culprits when it comes to high memory usage on Linux. One of the main offenders is Java. Whether you’re using the official Java runtime environment or the GNU-supplied alternative, this can cause you trouble. If you’re running into this on a server, it could be caused by JBoss or Tomcat. You’ll find their configuration files somewhere like the following:

Other culprits can be Apache or MySQL. It’s beyond the scope of this article to show you exactly how to configure these, but you can check your log files for errors or warnings from these services.

Conclusion

If you’re looking for performance gains or to fix an issue, memory may not always be the culprit. It depends on what you’re looking for, but your solution could lie in another part of the computer entirely. Make sure you keep this in mind.

Don’t worry, as no matter what the trouble is, we can help you out. If your desktop, laptop, or server is running slow, take a look at our guide to finding the cause. Even if it doesn’t solve your problem, it will likely help you find where to start.

3 comments

  1. Stop high CPU usage in Linux 18.04.3 by opening Terminal (CTRL+Alt+T) and copying and pasting the following text and hit enter and then password if asked.
    sudo systemctl restart systemd-udevd systemd-udevd-kernel.socket systemd-udevd-control.socket

    You will have to do this on every boot. I cannot find a way to do it automatically. Create a document on your desktop and past the text line into the document so it is available to be copied on each boot.

    1. There are a lot of people who are not using distros with systemd. So, your “solution” would not work for them.

  2. You can free up memory using:

    echo 1 > /proc/sys/vm/drop_caches ;echo 2 > /proc/sys/vm/drop_caches ;echo 3 > /proc/sys/vm/drop_caches

    from time to time or everytime your system is using more memory than should be…
    You can create a file inside /etc/cron.hourly, named freemem.sh with following

    nano freemem.sh
    #!/bin/bash
    echo 1 > /proc/sys/vm/drop_caches ;echo 2 > /proc/sys/vm/drop_caches ;echo 3 > /proc/sys/vm/drop_caches

    Save it and than make it executable:
    chmod +x freemem.sh

    Now cron will run the command every time!

    Cheers

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.