22 October 2018

Is Linux Eating Your RAM? How to Manage Your Memory


linux-eating-ram

So you’ve installed Linux on the promise that it requires fewer system resources than Windows. Why, then, is your system suddenly low on RAM?

Don’t panic! Linux isn’t eating your RAM. To understand just what’s going on, we’ll have to take a look at how Linux manages memory.

How to View Free RAM on Linux

As with anything on Linux, there are multiple ways to check how much RAM is in use. If you’re coming from a commercial operating system, you may find it easier to open an app similar to the Windows Task Manager or the macOS Activity Monitor. This program’s name depends on which Linux desktop environment you use.

On GNOME, the most widely-used option, open a program called the System Monitor. On KDE, a similarly feature-complete alternative, use Konsole instead. Other desktop environments have similar tools under various names.

For an approach that works regardless of your desktop environment, you can turn to the command line. Open up the Terminal application and type in the following command.

free

You will see results that look something like this. The numbers will change depending on how much RAM your machine has and the number of active programs.

A Linux terminal displaying the free command

For a more thorough look at your RAM, try this:

cat /proc/meminfo

The above command loads the same file that the free command uses to see how much memory is available on your PC.

If your numbers seem stark, it may seem that Linux has eaten your RAM. Let’s say you have 4GB of RAM, and 3.9GB is in use. Only 0.1GB appears to be free for additional apps! It seems you’re one program away from bringing your PC to a screeching halt.

Look again. This time turn your eyes to the “available” column. There you may find that you actually have 1GB of data open to apps. But how? It all comes down to how Linux manages and uses RAM.

How Linux Uses RAM

How Linux uses RAM isn’t much different from Windows and other operating systems. But different systems present what’s going on in different ways.

When your PC sees that there is RAM not currently in use, it dedicates some of this space to disk caching. In this way, apps store data in a location that’s quicker to access, making the system run faster and more smoothly. When other apps need more memory, they take the amount they need from this disk cache as though it were sitting around unused.

In other words, Linux puts that empty space to good use during its time off. Why let all that free memory go to waste?

To know how much space is in use this way, look at the “buff/cache” column. This is the amount of RAM dedicated to buffers and cache at the time you ran the command. The idea that clearing this space might help performance is a common misconception about RAM.

The Difference Between Free and Available RAM

Now that you’re all caught up, it’s still easy to get confused. I understand. Here’s how Linux views your RAM.

  • Used: RAM that is currently in use by an application.
  • Available: RAM that may be in use for disk caching but can be freed up for applications.
  • Free: RAM that is not in use by an application or disk caching.

Free and available may seem like synonyms, but there’s a technical difference. The former is memory that isn’t in use at all. The latter is memory that is currently in use but can easily open up if needed. For everyday use, it’s the latter that really matters.

How to Clear RAM Memory Cache and Buffer

Let’s say, for whatever reason, you still want to clear up RAM that the Linux kernel is using for its buffers and cache. To be clear, doing this will negatively impact performance. If you want your system to run optimally, leave things be.

You’re going to run these commands as a system administrator.

To clear pagecache, enter the following command:

echo 1 > /proc/sys/vm/drop_caches

To clear dentries and inodes, change the number to 2:

echo 2 > /proc/sys/vm/drop_caches

To clear pagecache, dentries, and inodes all together, change the number to 3:

echo 3 > /proc/sys/vm/drop_caches

If these commands don’t improve how smoothly your PC runs, why run them? Doing so is a way to compare your computer’s performance after making a change without needing to restart. If you’re not benchmarking or testing your PC, there’s little reason to bother.

How to View and Close Programs

Does all of this mean that you don’t need to check RAM and manage memory on Linux? Not quite.

While Linux knows how to optimize RAM, that doesn’t mean your apps do. Sometimes your browser will hog all your memory. A game might freeze without freeing up any system resources.  An image viewer might hang up when trying to load an extremely large picture.

To see which programs are giving your computer a hard time, try out the top command:

top

The results will look something like this.

A Linux terminal displaying the top command

Now that you’ve spotted a problematic program, what can you do about it? One option is to use the kill command. To do so, note the PID number that appeared next to the program when you enter top. You’re going to need it.

kill -9 2470

Alternatively, you can use the killall command to close a program using its name.

killall firefox

Boosting Linux System Performance Even Further

Unlike many things in life, your PC’s memory more or less manages itself. Here we’ve looked at ways to see what’s going on and, when action is needed, step in. You now know how to see how much memory is in use and what programs are using it. You can also take a few steps to stop activities that are hogging space.

But the tools above are hardly the extent of what’s available in the Linux toolbox. See our articles on speeding up a slow Linux computer and updating the Linux kernel for better performance.

Read the full article: Is Linux Eating Your RAM? How to Manage Your Memory


Read Full Article

No comments:

Post a Comment