When is swap space used




















This is a good thing - it helps speed things up heaps. Both writing to disk and reading from disk can be sped up immensely by cache. Ideally, you have enough memory for all your applications, and you still have several hundred Mb left for cache. In this situation, as long as your applications don't increase their memory use and the system isn't struggling to get enough space for cache, there is no need for any swap. Once applications claim more RAM, it simply goes into some of the space that was used by cache, shrinking the cache.

De-allocating cache is cheap and easy enough that it is simply done in real time - everything that sits in the cache is either just a second copy of something that's already on disk, so can just be deallocated instantly, or it's something that we would have had to flush to disk within the next few seconds anyway. This is not a situation that is specific to Linux - all modern operating systems work this way. The different operating systems might just report free RAM differently: some include the cache as part of what they consider "free" and some may not.

When you talk about free RAM, it's a lot more meaningful to include cache, because it practically is free - it's available should any application request it. On Linux, the free command reports it both ways - the first line includes cache in the used RAM column, and the second line includes cache and buffers in the free column.

Once you have used up enough memory that there is not enough left for a smooth-running cache, Linux may decide to re-allocate some unused application memory from RAM to swap.

It doesn't do this according to a definite cut-off. It's not like you reach a certain percentage of allocation then Linux starts swapping. It has a rather "fuzzy" algorithm. It takes a lot of things into account, which can best be described by "how much pressure is there for memory allocation".

If there is a lot of "pressure" to allocate new memory, then it will increase the chances some will be swapped to make more room. If there is less "pressure" then it will decrease these chances. Your system has a "swappiness" setting which helps you tweak how this "pressure" is calculated. It's normally not recommended to alter this at all, and I would not recommend you alter it. Swapping is overall a very good thing - although there are a few edge cases where it harms performance, if you look at overall system performance it's a net benefit for a wide range of tasks.

If you reduce the swappiness, you let the amount of cache memory shrink a little bit more than it would otherwise, even when it may really be useful. Whether this is a good enough trade-off for whatever problem you're having with swapping is up to you.

You should just know what you're doing, that's all. There is a well-known situation in which swap really harms perceived performance on a desktop system, and that's in how quickly applications can respond to user input again after being left idle for a long time and having background processes heavy in IO such as an overnight backup run.

This is a very visible sluggishness, but not enough to justify turning off swap all together and very hard to prevent in any operating system. This is not a situation that's limited to Linux, either. When choosing what is to be swapped to disk, the system tries to pick memory that is not actually being used - read to or written from. It has a pretty simple algorithm for calculating this that chooses well most of the time.

If you have a system where you have a huge amount of RAM at time of writing, 8GB is a huge amount for a typical Linux distro , then you will very rarely ever hit a situation where swap is needed at all. You may even try turning swap off. I never recommend doing that, but only because you never know when more RAM may save you from some application crashing.

But if you know you're not going to need it, you can do it. The act of transferring data from RAM to swap is a slow operation, but it's only taken when the kernel is pretty sure the overall benefit will outweigh this. It's also a last resort should your applications actually request more memory than you actually have. In this case, swapping is necessary to prevent an out-of-memory situation which will often result in an application crashing or having to be forcibly killed. Swapping is only associated with times where your system is performing poorly because it happens at times when you are running out of usable RAM, which would slow your system down or make it unstable even if you didn't have swap.

So to simplify things, swapping happens because your system is becoming bogged down, rather than the other way around. Transferring data out of swap is for traditional hard disks, at least just as time-consuming as putting it in there. So understandably, your kernel will be just as reluctant to remove data from swap, especially if it's not actually being used ie read from or written to.

If you have data in swap and it's not being used, then it's actually a good thing that it remains in swap, since it leaves more memory for other things that are being used, potentially speeding up your system. Setting the swappiness value doesn't work in every situation. If it works for you, great. If not, I've written a script to periodically clear out swap by turning it off and back on again. Toggling swap is a bit risky if you're not careful.

If you don't have enough free RAM to hold everything in RAM plus everything in swap, trying to disable swap will cause your system to become unresponsive. My script first checks whether there's enough free RAM which takes a bit of doing, as the actual amount of free RAM is different from what free reports as free , then only toggles swap if so.

But, if you're a bit short on RAM, don't start another major process while the script is running. Here it is:. You must run this script as root e. This script won't leave your system unresponsive; if you've got insufficient RAM, it will refuse to toggle swap.

I've used this script without problems for close to five years now. I edited the script of Scott Severance to match the newer versions of free which already include a total available memory field. Generally swap remains unused on now-a-days systems. In my experience, the processes which are running for a long time without intensive operations are shifted to swap by the linux.

It makes a few affected programs run slow. If you have lots of ram free, you may switch the swap off by running the command: swapoff -av you'll need sudo rights for it. If you don't like the swap off, you may switch it on, using symmetrical command: swapon -av again sudo required. The obvious suspicion was Situation 2 described above. For a normal system of ours, the comparison of zone info goes this this. Memory fragmentation is also a reason why swap usage can increases even when free memory is there.

Like everyone said, yes swap will help you get rid of unused memory, so it can help you having more memory available. But swap can also be used for hibernating which can be really useful when you have a laptop or want to save energy and put your computer and work in hibernation before leaving work. So you can have a quicker start the morning after. Having a hibernating function is one of the main reason we still see nowadays advise to have at least the size of RAM for the swap. That way the system can put all used RAM into the swap and goes into hibernation.

Take care that once swapped a process data could be read in the swap even after shutdown, unless the swap was encrypted of course. Using encrypted swap with hibernation doesn't work out-of-the-box with all distributions. A lot of modern programs are built on bloated frameworks that drag in a lot of junk you don't actually need in order to run the program.

Last year, I switched one of my web sites to a promising new web server framework that was built on top of Firefox. It may sound strange to build a server-side system on top of a client-focused program like Firefox, but it had some huge benefits. Firefox is very powerful, offers some really impressive internal services, and it reduces the impedance mismatch between server and client to have both running similar platforms.

But there's a downside: Firefox is big. Really big. This was a version 1. I ended up needing MB RAM minimum just to run the site without it crashing due to memory exhaustion. Think mashups. A lot of that sort of thing would break if you can't "render" the web page into some graphical context. By the way, this web framework is essentially dead now, so there's no point name-and-shaming it.

Best to just take the broader lesson to heart: yes, swap is still useful even if you have gigs of free RAM. I think "Gilles" already mentioned the fact that, while you may have more than enough RAM, swap can be useful during certain "shortcomings" as well as persistently saving some data even after shutdowns--or am I wrong in assuming that?

At one point, when I had disabled all swap and was only relying on my RAM, I had painfully difficult experiences trying to debug some system error, or crash, etc. Since then, I have re-enabled the swap partition.

From Ubuntu Swap F. As a base minimum, it is highly recommended that the swap space should be equal to the amount of physical memory RAM. Also, it is recommended that the swap space is twice the amount of physical memory RAM depending upon the amount of hard disk. I think you should increase your swap space in your system. The swap speeds up RAM memory allocation by allowing to discard already paged data.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Why use swap when there is more than enough free space in RAM? Ask Question. Asked 11 years, 1 month ago. Active 6 months ago. Viewed k times. Checkout my conky output below: Also, could this be the cause of speed and system-responsiveness issues I'm having?

Improve this question. Bleeding Fingers 3 3 gold badges 11 11 silver badges 23 23 bronze badges. Stefan Stefan 23k 39 39 gold badges 93 93 silver badges bronze badges. I'm pretty sure the dynamics of this issue have significantly changed with SSDs becoming the norm. SSD while slower is a lot cheaper and in most cases fast enough so even swapping shouldn't significantly disturb the user experience like it used to with rotational media.

Sometimes, if you used swap in the past because of full RAM, you can have a situation where previously swapped data stay there because it is not usefull data at the moment. As Totor said. Sometimes the system will page something out for whatever reason.

If later that page is moved back to memory for a read operation, the copy in swap space is not deleted. If the same page is later paged out again, without being changed, it can do so without writing to the disk again.

The copy that is there is already up to date. In other words, a page can take up space both in swap and main memory. Add a comment. Active Oldest Votes. To change the value temporarily lost on reboot : sudo sysctl vm. Improve this answer.

Stephen Kitt k 43 43 gold badges silver badges bronze badges. Marcel Stimberg Marcel Stimberg 3, 1 1 gold badge 16 16 silver badges 14 14 bronze badges. Note that reducing swappiness does not necessarily mean a performance or responsiveness increase. I've seen reports of increasing swappiness translating into better performance. Don't believe anything you read that doesn't include benchmarks, and check that the benchmarks use a workload similar to yours.

Does this persist across reboot? However, given that swap space is usually designated at install time, and that it can be difficult to determine beforehand the memory workload of a system, we recommend determining system swap using the following table. Table 7. File systems and LVM2 volumes assigned as swap space cannot be in use when being modified. For example, no system processes can be assigned the swap space, as well as no amount of swap should be allocated and used by the kernel.



0コメント

  • 1000 / 1000