Linux уменьшить использование swap

How to reduce size of swap after a system is already installed?

I’m running Debian Squeeze 6.0.5. Does the use of swap memory make my computer run slower? If so, how can I reduce the size of the swap memory after the system is already installed?

4 Answers 4

One doesn’t always want to reduce it, but often to increase its lazy usage instead — the more clean pages are already in swap, the better, it means they can easily be set off RAM when free RAM is needed. Linux VM, though, has some weird behavior regarding swapping — intensive disk I/O (like huge file cp ) can make your system swap unwanted heavily. It can be mitigated to some degree by decreasing vm.swappinness and increasing vfs_cache_pressure although the effect of such countermeasures isn’t always meeting expectations. I think it also makes sense to mention zswap here — for some workloads it can be useful.

@palik sysctl-explorer is a nice idea, but it uses outdated reference material; the kernel docs are more accurate.

To turn off swap temporarily, use (as root) the command:

To turn it off permanently, edit the file /etc/fstab and comment out any lines with swap in the type column.

More swap does not always improve performance. There are times when reducing it or turning it off is better — it all depends on the mix of applications.

I did a quick google and came up with this good article on the subject:

swap serves basically two purposes. It allows the system to continue to operate when physical memory runs out at a performance cost — run out of physical memory without it, you get crashes, lockups, and processes being killed with out of memory errors the second they ask for more memory than the system has. The reduced performance in this case is a symptom of being forced to use it for active processes, rather than a symptom of having too much swap.

It also lets physical memory be used more efficiently, by moving less-used pages in memory to disk until they are needed again. This frees up memory for cacheing purposes, which is usually a more efficient use of space than having infrequently used segments of program memory just sitting there locked in physical ram.

A long-standing best practice has been to size swap space at twice the physical memory, in other words, if you have 1GB of ram, devote 2GB to swap. This is still good advice, but in practice more modern systems with 4GB or more of physical ram can usually drop this to the same amount of swap as the system has of physical memory.

There are a few things you can do to improve performance when using swap. If you have multiple drives, moving swap to a faster or less used hard drive is recommended, and on a very IO-bound system, you may get significant performance increases by doing this. For traditional hard drives, moving swap closer to the center of the physical disk may help, as seek times are generally shorter at the center due to less travel of the drive heads.

Having swap on an SSD can help significantly as well, but I’d caution you that this may create a lot of wear and tear on a SSD, and will give it a shorter lifespan.

Читайте также:  Линукс минт последний релиз

Of course, the best solution to improving memory performance is usually to throw more RAM into the box, and if you look at your memory usage and see heavy usage of swap along with little or no free memory, it’s a good indication that it’s time to invest in more ram.

Источник

How to clear swap memory in Linux

box of old photos

Swap memory is usually a «set it and forget it» type of affair. Most enterprise environments have swap built into the systems, and these memory caches are not manipulated unless there is an apparent lack of memory available or if a server crashes due to the OOM killer (out of memory) error. However, there is a niche situation that can cause an administrator to need to clear the system swap manually. If that is the situation that you find yourself in, you’ve come to the right place. This article is a discussion about this situation and the solution required.

Feeling swappy?

Occasionally, a system uses a high percentage of swap memory even when there is RAM available for use. The culprit here is the ‘swappiness’ of the system. Yep, you read that right. swappiness. So now that you know the lingo, you’re ready to explore what it means. Swappiness refers to the kernel parameter responsible for how much and how often that the system moves data from RAM to swap memory.

The default value for swappiness is 60; however, you can manually set it anywhere between 0-100. Small values cause little swapping to occur, whereas high values can cause very aggressive swapping. A value of zero causes no swapping at all to occur, so if you want to minimize swapping to its lowest possible value without turning it off, you should set it to at least one.

If you wanted to change up the swappiness of your system, the procedure is very straight-forward. You can check your current swappiness setting by running the following command:

It should look something like this:

output of cat /proc/sys/vm/swapiness

Now, you can see that my system has a swappiness value of 30. To alter the value, you want to use the following:

$ sudo sysctl vm.swappiness=x (where x is the swap value you wish to set)

output of sysctl vm.swappiness=60

To verify the value that you set, simply cat the swappiness file that you looked at earlier to find out the original value. Easy day. Now that you understand the underlying parameters that control the swap behavior on our system, you’re ready to learn how to clear that memory, should the situation arise. For the first time in your terminal life, things are going to be easy here.

[ For more information on swap space, see Managing swap in the modern Linux system. ]

To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back into RAM. It also means that you need to be sure you have the RAM to support this operation. An easy way to do this is to run ‘free -m’ to see what is being used in swap and in RAM. Once you power it off, you can wait an arbitrary amount of time (30 sec or so) to give the operation time to complete, then power the swap back on. This clears the swap memory cache and re-enables it. Here are all of the commands you’ll need!

Check space: # free -m Disable swap: # swapoff -a Wait approx 30 sec (use free -m to see the amount of swap used/available decrease over time) Enable swap: # swapon -a 

Hopefully, this quick tip helps you clear your system swap memory if you ever find yourself in need of just such a fix.

Читайте также:  Compiled dll on linux

[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]

Источник

How to Manage Swap Usage in Linux

How to Manage Swap Usage in Linux

You may have experienced system lags, heavy swap usage, or low memory issues in your Linux system. A usual advice is to “decrease the swappiness value”, reduce the swap usage. but what is swappiness? And is it really good to tweak it?

Note: we won’t go into detail about the swap partition. You can read all about it in our article on what you need to know about Swap partition on Linux.

Swappiness

There is a value called swappiness in UNIX-like systems that determines how pages in memory will be handled. Its default value on most systems is “60,” but it can be set to anything between 0 and 100. If memory runs low, the kernel will either evict some file caches to have more free RAM for processes, or it will swap some process pages from RAM to disk.

A default of 60 means swapping will be used less, and I/O caches will more likely be freed for reallocating RAM. Evicting caches is considered “cheaper” (less resource intensive), while swapping out pages includes disk reads and writes, making them more “expensive.” At a value of ’60,’ swap usage will be slightly lower than cache reuse. If the value of swappiness is increased to 100, swapping and file cache eviction will be used with equal weight. This means more swapping and faster I/O than default. Lower values like “10” mean that swapping will be used much less, and I/O caches can be much sooner evicted in favor of processes. This might increase interactivity but could also hurt I/O speed.

Decreasing swappiness

You will often see the advice on websites and forums to reduce swappiness to around “10.” This is supposed to speed things up by using less swap and keeping more processes in physical RAM. This logic might be somewhat too straightforward and might not be a “one size fits all solution.”

Decreasing the value of swappiness might be good in the following scenarios:

  • For databases systems, or if you use a lot of database intensive applications: Databases generally handle file caching way better than the OS would. If you reduce swappiness, you will limit the OS’s file caching, thus giving a chance to the database to handle its own caches.
  • For interactivity: If you multitask a lot but handle few files or don’t open large documents, this could improve how smooth your system will feel. Less processes will be written out to disk, and as RAM access is much faster, your computer can feel faster. You must also bear in mind that this can reduce I/O performance. If you have slow disks or do anything I/O intensive, it might even hurt performance.
  • For placebo: Many believe that a lower swappiness will speed up the system,. Seriously, f you think that your system is faster, it might have the effect of perceiving it to be faster, too.

Keep in mind that decreasing swappiness might result in crashes and processes being randomly killed by the system in order to free up memory. Decreasing swappiness will be best if you have enough RAM available to run your system smoothly. But you should keep the value above “10”, as some swap use is good to have.

Читайте также:  Ограничить нагрузку процессора linux

Increasing swappiness

Increasing swappiness can have the general advantage of speeding up I/O. Although not often advised, increasing swappiness might come in handy if:

  • You perform some I/O intensive operations, and you have slow, old HDDs. For example: performing backups or batch editing images can be I/O intensive (A notable exception is databases which are naturally I/O intensive but could benefit from a lower swappiness value as seen above.).
  • You have low memory but have relatively fast disks. In this unlikely scenario, a higher value of swappiness might help to handle memory more efficiently (although setting it too high might again hurt performance.).

How to manage swappiness

First, you should consider if you need to touch this value at all. Are you experiencing performance issues? And more importantly, have you tried other system tweaks yet? If not, it is probably best to look elsewhere first. But if you have a specific scenario in mind, you might want to continue.

Next, how heavy is your swap usage? You can find it out with any performance monitoring tool of your choice or with the free command. free -m will give you a snapshot of the memory usage in megabytes. For continuous monitoring, you might want to use watch.

This will run the free -m command every second and print its output until you press “Ctrl + C.”

swappiness-free-m

As you see above, very little swap is being used by the system this article is written on.

Now if you do experience swapping, you might be interested how much of it is active. The command vmstat will tell you all you need to know about your system’s virtual memory usage (swap and physical ram together).

swappiness-vmstat

You need to check the swap column where si means “swap in”, and so means “swap out.” If the numbers are high, it means a lot of swapping activity which is an indicator of low memory issues. If you see swap usage by free but little active swapping, tweaking swappiness might be due.

It is also a good idea to establish other performance benchmarks like disk I/O, load averages, etc., so when you test your new swappiness values, you have something to compare against.

To test a different swappiness value, you can set it temporarily with the sysctl command. This needs no rebooting, and the effect is immediate. In fact, rebooting will restore the default value, so you are quite safe to experiment:

sudo sysctl vm.swappiness=10

Of course, you can put any value instead of “10” (between “0” and “100”).

Once you’ve found your preferred value, you can permanently change the system configuration by editing “/etc/sysctl.conf”

#Set swappiness value vm.swappiness=10

to the end of this file, with the value set to your preference once again, of course.

Conclusion

Lowering swappiness to “10” is often advised as a one-size-fits-all solution, but the actual use of the technique might be a tiny bit more complicated than that. By establishing benchmarks, knowing your system, how you use it, and what you need from it, you can fine tune your swap usage and achieve some performance (either interactivity or I/O) increase.

Attila is a writer, blogger and author with a background in IT management. Using GNU/Linux systems both personally and professionally, his advice stems from 10+ years of hands on experience. In his free time he also runs the popular Meditation for Beginners blog.

Our latest tutorials delivered straight to your inbox

Источник

Оцените статью
Adblock
detector