amuck-landowner

Why swapping when there is free memory?

vanarp

Active Member
There is swapping happening on my VPS when there is still free memory available. I always thought swapping does happen when the physical memory cannot accommodate the need. Appreciate your help to understand this.

Code:
free -h
             total       used       free     shared    buffers     cached
Mem:          128M        96M        31M         0B         0B        62M
-/+ buffers/cache:        34M        93M
Swap:         128M        28M        99M
 

peterw

New Member
Look to your process list. There should be inactive processes. The Linux kernel prefers to swap out inactive pages of threads because the memory is better used as cache. Maybe someone knows the sysctl parameter?
 
Last edited by a moderator:

concerto49

New Member
Verified Provider
Look to your process list. There should be inactive processes. The Linux kernel prefers to swap out inactive pages of threads because the memory is better used as cache. Maybe someone knows the sysctl parameter?
Exactly, despite all the hate - swapping might not be a bad thing. On a dedicated server it's a normal process. It happens on servers and desktops too. The OS makes an intelligent choice based on the situation. Often taking up RAM doesn't mean it'll be faster, especially if the data might not be used at all.
 

drmike

100% Tier-1 Gogent
Adjust your swapiness.  Comes default at 60.  I typically lower it to 10.  My personal workstations, I lower it to 0.

Nothing cool or right about swapping when you have RAM available.  Unless you like disks turning and burning. 

Yeah, the OS knows best, sure.  Value of 60 they use is horrifically high and many issues in desktop environment with such value.  Just got done dropping that on a netbook someone uses and went from terrible performance and IO WAIT to pretty okay.
 

peterw

New Member
So it is this value?


cat /proc/sys/vm/swappiness
60

Set vm.swappiness=10 in /etc/sysctl.conf and reboot.
 

Master Bo

Member
I agree, decrease swappiness. Type
 


sysctl vm.swappiness
to see current level. If I have 2-4 Gb of RAM, I usually do not set it above 10. Higher value is sensible only when VPS is low of memory. To change the value on the fly:
 


sysctl -w vm.swappiness=10

and for permanent effect, add the


vm.swappiness = 10
line to sysctl config file (usually /etc/sysctl.conf), or correct existing line for the same parameter. You can apply all new configuration parameters on the fly with

Code:
sysctl -p
 
Last edited by a moderator:

vanarp

Active Member
Here is the complete process list:


root@ram128:~# ps axjf
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 1 1 1 ? -1 Ss 0 0:06 init [2]
1 2 0 0 ? -1 S 0 0:00 [kthreadd/5997]
2 3 0 0 ? -1 S 0 0:00 \_ [khelper/5997]
1 1675 1675 1675 ? -1 Ss 0 0:01 /usr/sbin/cron
1 1864 1864 1864 ? -1 Ss 0 0:21 sendmail: MTA: accepting connections
1 5275 5275 5275 ? -1 Ss 0 0:00 /usr/sbin/sshd
5275 31433 31433 31433 ? -1 Ss 0 0:00 \_ sshd: vanarp [priv]
31433 31435 31433 31433 ? -1 S 1000 0:01 \_ sshd: vanarp@pts/0
31435 31436 31436 31436 pts/0 767 Ss 1000 0:00 \_ -bash
31436 621 621 31436 pts/0 767 S 0 0:00 \_ sudo su
621 622 621 31436 pts/0 767 S 0 0:00 \_ su
622 623 623 31436 pts/0 767 S 0 0:00 \_ bash
623 767 767 31436 pts/0 767 R+ 0 0:00 \_ ps axjf
1 20801 20801 20801 ? -1 Ss 0 0:19 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
20801 20802 20801 20801 ? -1 S 33 0:00 \_ php-fpm: pool www
20801 20803 20801 20801 ? -1 S 33 0:00 \_ php-fpm: pool www
1 23993 23896 3159 ? -1 S 0 0:00 /bin/bash /usr/bin/mysqld_safe
23993 24346 23896 3159 ? -1 Sl 103 0:00 \_ /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/
23993 24347 23896 3159 ? -1 S 0 0:00 \_ logger -t mysqld -p daemon.error
1 25068 25068 25068 ? -1 Ss 0 0:00 nginx: master process /usr/sbin/nginx
25068 25069 25068 25068 ? -1 S 33 0:14 \_ nginx: worker process
25068 25070 25068 25068 ? -1 S 33 0:36 \_ nginx: worker process
25068 25071 25068 25068 ? -1 S 33 0:32 \_ nginx: worker process
25068 25072 25068 25068 ? -1 S 33 0:26 \_ nginx: worker process


And here is the output for swappiness check:


root@ram128:~# sysctl vm.swappiness
vm.swappiness = 5


Output of ps_mem.py:


root@ram128:/home/vanarp# ./ps_mem.py
Private + Shared = RAM used Program

76.0 KiB + 45.0 KiB = 121.0 KiB logger
120.0 KiB + 29.5 KiB = 149.5 KiB init
164.0 KiB + 42.5 KiB = 206.5 KiB cron
284.0 KiB + 194.0 KiB = 478.0 KiB mysqld_safe
448.0 KiB + 169.0 KiB = 617.0 KiB su
644.0 KiB + 166.0 KiB = 810.0 KiB sudo
904.0 KiB + 60.0 KiB = 964.0 KiB sendmail-mta
652.0 KiB + 626.0 KiB = 1.2 MiB bash (2)
944.0 KiB + 728.0 KiB = 1.6 MiB nginx (5)
952.0 KiB + 832.0 KiB = 1.7 MiB sshd (3)
356.0 KiB + 3.2 MiB = 3.5 MiB php5-fpm (3)
17.1 MiB + 114.0 KiB = 17.2 MiB mysqld
---------------------------------
28.6 MiB
=================================


This VPS has 128MB with 128MB vSwap assigned. Do you see a problem?
 

peterw

New Member
Can you run cat /proc/meminfo?

Run top press capital letter o, then p, then enter. Top will display processes sorted by their swap usage.
 

mtwiscool

New Member
Most webservers will swap if the swap is available.


Swapping is the OS optimizing itself.


In other words it's nothing to be scared of unless the program you are running was coded right.
 
Top
amuck-landowner