Swap used even when ram is available after memory usage spike
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian (Ans soon Ubuntu and Android)
Posts: 48
Rep:
Swap used even when ram is available after memory usage spike
Hello,
now, I don't have much experience when it comes to swap, so please forgive me if this post doesn't make sense.
Basically, after a memory usage spike and closing down some apps, I noticed that my system still uses swap space even if there's now ram available, even with actively used processes.
(Currently using around 1/3rd of ram available, while still using around 180 MiB swap.)
Sometimes this makes things in my system extremely slow (really old machine, slow hard drives...).
When there is ram available, shouldn't the system put what is in the swap back in the ram?
If that's not default behavior, can it be enabled?
It's expensive to continually run the allocated/free memory chains - so it isn't done unless necessary.
What you can try to force the swap back into memory - the swapoff will simply fail if it won't fit in memory, no damage done.
Code:
swapoff /dev/sda?
swapon /dev/sda?
Substitute the appropriate partition - "swapon -s" will show that.
Some swap is always in use - at least the way it usually works. Even with my "working" machine (Fedora 18, 6 Gigs of RAM) there is some data stored in the swap.
Changing this behavior is called "changing swappiness". I found something on the internet, but it's for Ubuntu: https://help.ubuntu.com/community/SwapFaq
I think it will work for Debian the same way, as Ubuntu is based on Debian (And let's be honest, it IS Debian with a few simplifications in configuration and day to day usage).
Hope I could help.
I do not think swap is used by active processes, swap is only used in case of "out of memory". Pages in use are always located in the main memory. Unused pages will not be copied back to the memory (as long as it was not requested by the system).
Basically, after a memory usage spike and closing down some apps, I noticed that my system still uses swap space even if there's now ram available,
That is perfectly normal. There is no reason to try to "fix" it.
Quote:
even with actively used processes.
Even if every process is active (which I doubt), not every memory page of every process is active. Any page in swap that gets accessed won't be in swap any more.
Quote:
Sometimes this makes things in my system extremely slow (really old machine, slow hard drives...).
WRONG!
Maybe the swap use makes you imagine the system is slow. Maybe the system is slow for some other reason at the same time swap is in use. But the swap use does not make your system slow.
Quote:
When there is ram available, shouldn't the system put what is in the swap back in the ram?
It doesn't and it shouldn't. Pages are read back from swap when those pages are used. No matter how much free memory you have, there is no point reading back a page that isn't being used.
Without that extra free memory, when it reads one page from swap, it must write another to swap. With the excess free memory, it can read pages from swap if/when they are used, and not write anything back to swap.
Maybe the swap use makes you imagine the system is slow. Maybe the system is slow for some other reason at the same time swap is in use. But the swap use does not make your system slow.
I'd like to understand your line of reasoning about this, John. Since access time for a page on disk is several orders of magnitude slower than for a page in resident memory (milliseconds vs nanoseconds), use of swap definitely elongates response times for affected programs. If thrashing is occurring, the effect on overall performance is usually very noticeable.
I'd like to understand your line of reasoning about this, John. Since access time for a page on disk is several orders of magnitude slower than for a page in resident memory (milliseconds vs nanoseconds), use of swap definitely elongates response times for affected programs. If thrashing is occurring, the effect on overall performance is usually very noticeable.
I was assuming the pages in question were still in swap AFTER the slowness of the system was observed, despite sufficient free ram.
That would mean those pages were not accessed. If they aren't accessed, then the time it would have taken to access them doesn't matter.
The important thing to distinguish here is between space being occupied in swap (no ongoing performance cost and no indication of a problem) vs. continuing swap-in/swap-out activity, which would be a significant performance hit.
The OP did not quote the right stats to directly see the level of swap in/out activity. But Linux is pretty reliable about reasonable behavior regarding swap. So we can infer a low swap in/out rate from the free memory.
I know what Z038 meant; Johnsfine's one-liner comment just there would have been better with some context.
As he pointed out, if the use of swap (mem spike) has passed, then the kernel will only swap pages back into RAM IF it wants them.
No affect on system performance by leaving them on swap if not reqd.
OTOH, if swap is being actively used, then yes, that will be magnitudes slower than a RAM read/write.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.