0

My PC has swappiness set to 60.

cat /proc/sys/vm/swappiness
60

Even so, swap only occurred when the PC runs out of RAM. So, every time I fill the RAM, it stops responding for a while while it is swapping into its HDD.

I thought that with swappiness of 60, proximately 60% of (full size of) RAM would be mirrored on swap so it is already there when needed to be swapped out... But that is not what I am experiencing.

I've read the manual and many different answers here but none is about having swappiness on the right value and swap not occurring until the very last moment.

Contents of free -h

              total        used        free      shared  buff/cache   available
Mem:            15G        9,9G        266M        4,7G        5,4G        508M
Swap:           30G        3,2G         27G

swapon:

NAME      TYPE       SIZE USED PRIO
/dev/dm-1 partition 30,5G 3,5G   -2
brunoais
  • 165
  • 2
  • 14
  • 2
    afaik swap is not a mirror but more like an extension of your ram. – pLumo Jan 14 '19 at 12:42
  • @RoVo Is it more clear with the changes I did? – brunoais Jan 14 '19 at 12:44
  • How much RAM and Swap space do you have, and what makes your machine run out of RAM? – Soren A Jan 14 '19 at 14:46
  • Edit your question with the output of free -h and swapon. Lower values for vm.swappiness will reduce swapping... assuming that you have enough RAM. Higher values for vm.swappiness will increase swapping... possibly better for really low RAM configurations. It can be temporarily set with the sysctl command, or edited into a conf file to be permanent. – heynnema Jan 15 '19 at 15:54
  • @heynnema Does that information help? – brunoais Jan 15 '19 at 17:59
  • 1
    @brunoais yes, thanks. Your swap is excessive, unless you're hibernating your comuter, but that's not a show stopper. You probably have plenty of RAM. Use top to see more. In terminal, try sudo sysctl vm.swappiness=80 and then run the machine for a while and see if there's any improvement. Due to more swapping, it may make it worse. If it's not better, then try the opposite... =10... a more common value when there's enough RAM. – heynnema Jan 15 '19 at 18:08
  • @heynnema Yes. Swap for hibernating. I have plenty of RAM but I also have memory hog programs (which do require to use that large RAM). At times, I open one of those huge programs and the system goes to a crawl while non-essential programs are swapped. I really don't understand why swap is only starting at the moment RAM is full and not way before such that removing 500MB from RAM cannot be instantaneous. Testing swappiness of 80 now. – brunoais Jan 15 '19 at 18:11
  • 1
    @brunoais please see my slight edits to my previous comment. If the top command confirms your thought about memory hogs, and vm.swappiness tweaking doesn't help... and it may not... you'll just have to get more RAM to solve it. – heynnema Jan 15 '19 at 18:14
  • @heynnema Increasing swappiness to 80 almost solved it. I then increased swappiness to 90 and the problem went away. I do get some slowdowns here and there but the overall responsiveness is being kept high. Feel free to transform your comment into an answer (also adding some details) – brunoais Jan 17 '19 at 08:20

2 Answers2

2

Your free -h shows...

              total        used        free      shared  buff/cache   available
Mem:            15G        9,9G        266M        4,7G        5,4G        508M
Swap:           30G        3,2G         27G

indicates that you have enough RAM and SWAP (you ARE using hibernation).

Tuning your system with regards to swap is a tricky procedure.

It involves:

  • how much RAM you have
  • how much SWAP you have
  • whether swap if FILE based or PARTITION based
  • what applications you run and how much RAM they use
  • whether you use hiberation, or not

Setting up appropriate swap space is open for large debate. GENERALLY speaking, swap space should be 50-120% of total RAM. Larger amounts of swap are used in a hibernation configuration.

Assuming that you have enough RAM and swap, vm.swappiness is a parameter that you can adjust to optimize the use of RAM vs swap. To see your current setting:

sudo sysctl vm.swappiness

it's normally set to 60.

To temporarily change this setting:

sudo sysctl vm.swappiness=nn

where nn=0 to 100.

Lower settings make more use of RAM and less swapping. Higher settings reduce RAM use and increase swapping.

After determining the optimum value for your configuration, to set it permanently, edit /etc/sysctl.conf. Add this value to the bottom of the file:

vm.swappiness=90

we've determined that 90 is a good value for your use. This setting MAY slow down your system a bit, as swapping is based on slow disk I/O.

then reboot.

jakethedog
  • 133
  • 5
heynnema
  • 70,711
  • In my case, it's better to have slowdowns here and there but keep the PC responsive than having complete pauses for many seconds just because there's a high need to swap. Thank you. – brunoais Jan 17 '19 at 18:10
0

Swappiness is just a parameter that the kernel uses to calculate a swap tendency. I read the manual and I was unable to find where it states that /proc/sys/vm/swappiness is some kind of threshold.

About this topic, Red Hat Knowledge Base states:

this parameter sets the kernel's balance between reclaiming pages from the page cache and reclaiming pages by swapping out process memory

Also, another answer in Ask Ubuntu states:

Swapping is a slow and costly operation, so the system avoids it unless it calculates that the trade-off in cache performance will make up for it overall, or if it's necessary to avoid killing processes.

A lot of the time people will look at their system that is thrashing the disk heavily and using a lot of swap space and blame swapping for it. That's the wrong approach to take. If swapping ever reaches this extreme, it means that swapping is your system's attempt to deal with low memory problems, not the cause of the problem.

Hence, I think you are working on a misconception about how swap and swappiness works.

  • I'm not having problems related to swap thrashing. It becomes unresponsive, then after ~1 minute sending ~1GB to swap, the system runs snappy again. – brunoais Jan 15 '19 at 17:57
  • This means that all cold memory is already in swap and the kernel needs to start swapping hot memory. There is nothing the system can do about that. The best you can do is setting swappness to a higher value and buy more RAM. – PEdroArthur Jan 17 '19 at 12:46
  • In my case, "cold" memory was still being kept in memory. I wonder if it is caused by the size of the swap. – brunoais Jan 19 '19 at 09:04
  • how did you measure that? – PEdroArthur Jan 19 '19 at 09:08
  • Nothing precise but by analysing to which process and thread had memory that hadn't been touched for a while and then manually checking the behavior of such programs manually and take notice when swap use was making the machine slower – brunoais Jan 19 '19 at 09:19