LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Swap used even when ram is available after memory usage spike (https://www.linuxquestions.org/questions/linux-newbie-8/swap-used-even-when-ram-is-available-after-memory-usage-spike-4175472433/)

werner291 08-07-2013 03:12 AM

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?

(Currently using Debian Wheezy as a desktop OS)

syg00 08-07-2013 03:18 AM

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.

thelinuxist 08-07-2013 03:19 AM

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.

pan64 08-07-2013 03:27 AM

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).

werner291 08-07-2013 05:26 AM

Thanks for the replies, I'll have a look if anything works.

johnsfine 08-07-2013 07:31 AM

Quote:

Originally Posted by werner291 (Post 5004626)
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.

werner291 08-07-2013 08:57 AM

@johnsfine Thanks for preventing me from trying stupid things.

Then there must be something wrong with the stuff I'm running on my system then... Some day I will find it.

Z038 08-07-2013 09:20 AM

Quote:

Originally Posted by johnsfine (Post 5004784)
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.

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.

johnsfine 08-07-2013 10:26 AM

Quote:

Originally Posted by Z038 (Post 5004878)
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.

chrism01 08-08-2013 07:46 PM

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.
:)


All times are GMT -5. The time now is 06:44 AM.