Swapiness and Grub Delay

Arch wiki → search “swappiness” →

https://wiki.archlinux.org/index.php/Swap#Swappiness

2 Likes

While you don’t have to have swap, it’s still highly recommended even when you have lots of RAM.
There’s always something that can fit in there :slight_smile:

2 Likes

Ok now I have two conflicting answers about if I should have a swap file or not.
I have one single NVME drive on the system and 16 Gig of RAM
I already reinstalled without a swap file/partition.

Do I now have to add one back?

There is another option as well if you opt out of swap partition or swapfile: zram
You could make one 512-1024 MiB in case the kernel is in dire need to swap some pages.

The zram driver creates a compressed block device in ram. That block device can then be used for swap or general purpose ram disk. The two most popular uses for it are swap to extend the available amount of ram to processes and /tmp. The ram used for the block device is dynamically obtained and released up to it’s predefined uncompressed maximum size. The way it extends the amount of available ram to a system is by using a portion of the ram as compressed swap. It can therefore hold more pages of memory in the compressed swap than the amount of actual memory used. Typically it compresses to a 3:1 ratio. So, 1G of swap uses only 333MB of ram on average. The compression ratio including memory used for disk overhead varies depending on the % maximum space used. I found it to vary from 1.5:1 for a 1.5G disk with only 5% space used, to over 3:1 when nearly full. It also is much faster at swapping pages than typical hard disk swap.
https://wiki.gentoo.org/wiki/Zram

Why all the swapiness? I don’t see swap even being used unless your equipment is Ancient. I think it’s better to use a swap file instead. Or none? Swapfile is needed for hibernate and suspend i think.

1 Like

I was under the impression that having a swapfile was a good thing, in general but I usually turned swappiness down to 10. Then someone mentioned that I should not have swap at all because I have an SSD (I’m assuming it has to do with wear & tear on the SSD).

I’m not completely new to Linux, but I’m new to ARCH based Linux, and I’ve got a brand-new computer system, I’m not as well versed as I used to be in hardware and modern “best practices” (20 years out of practice will do that I guess)

Well - FWIW, I think you have good instincts. I have a swap defined, always, as it allows for a better scheduling to be enabled. I run SSD (and nvme), but it doesn’t worry me much - they have gotten much better at handling writes. On top of that, the swap doesn’t get used - so there is not much threat the longevity of the SSD! - But you still get the other advantages. Either a swap partition (which just ‘wastes space’) or a swap file (zram is the better choice in that) will give you the advantages - even if not actually used (haven’t caught it being used in 10 years or so).

Hope this doesn’t confuse worse!

2 Likes

SO I guess now

How do I create a swap partition without re-isntalling
and how/should I reduce swappiness to 10 as mentioned in several SSD guides?

I want less Swapiness and Grub Delay and more EndeavourOS today! :laughing:

1 Like

Watch

Such swap. So GRUB. Much EndeavourOS.

5 Likes

Wow.

image

2 Likes

Regarding to swap or not to swap I think this article here is of interest:

Basically it says that swap is benefic even if sappiness is low, even on systems with high memory size.

You could look into systemd-swap for file based swap, which would allow for a dynaimcally-sized swap, without the need to provision a certain amount of space for it.

Regarding SSD and writes conservation, I’d say for your peace of mind, you can just stop thinking about it.

Here are my S.M.A.R.T stats (sudo smartctl /dev/nvme0n1 -a) for a 1TB nvme drive that I’ve been using for 8 months now. I use this drive for all my data, including my daily driver operating system itself, the swap file (using systemd-swap, even if the system has 32GB ram on it), work data, while also heavily downloading torrents on it (10GB to 30GB files):

SMART/Health Information (NVMe Log 0x02)
Critical Warning:                   0x00
Temperature:                        31 Celsius
Available Spare:                    100%
Available Spare Threshold:          10%
Percentage Used:                    1%
Data Units Read:                    19.943.809 [10,2 TB]
Data Units Written:                 21.281.323 [10,8 TB]
Host Read Commands:                 233.132.105
Host Write Commands:                260.803.781
Controller Busy Time:               4.398
Power Cycles:                       606
Power On Hours:                     3.547
Unsafe Shutdowns:                   51

That’s 10TB in 8 months. This SSD is rated for >600TB writes. I expect this PC to be out of commission before the SSD has its write capacity depleted. Those write limits are really only relevant for server usage where there is continuous writing. Of course, if the drive has a lower capacity, the write-ratings are lower, but still, that should be enough for many years.

1 Like

Wow,
That’s interesting because i just ran the command and what made me jump was the number of unsafe shutdowns. Because i have been doing so much testing and messing around and all the problems i have had with virtual-box and also Deepin locking up! I have hard started this system a lot.

Edit:

=== START OF SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

SMART/Health Information (NVMe Log 0x02)
Critical Warning: 0x00
Temperature: 40 Celsius
Available Spare: 100%
Available Spare Threshold: 10%
Percentage Used: 0%
Data Units Read: 6,033,380 [3.08 TB]
Data Units Written: 14,744,711 [7.54 TB]
Host Read Commands: 80,745,178
Host Write Commands: 74,510,107
Controller Busy Time: 79
Power Cycles: 567
Power On Hours: 1,670
Unsafe Shutdowns: 98
Media and Data Integrity Errors: 0
Error Information Log Entries: 0
Warning Comp. Temperature Time: 0
Critical Comp. Temperature Time: 0

Error Information (NVMe Log 0x01, max 256 entries)
No Errors Logged

I’ve edited my post and added the unsafe shutdowns count. Not really sure what registers as an unsafe shutdown. Hard reset maybe?

So, no word on creating a Swap partition after install?

Easy enough to do - if you think it warranted. Assuming there is space on whatever drive you wish to use, use GParted to create it as a swap - then sudo nano your /etc/fstab to include:

UUID=########-####-####-####-############      none             swap             defaults 0 0

where the UUID is what you get from sudo blkid - or whatever command you prefer using for that information. I would still (at this point) look into zram or systemd-swap instead though…

1 Like

Hmmmm…To affect swappiness & other factors, some of the other distros (Debian for example) links /etc/sysctl.d/99-sysctl.conf to a plaintext file /etc/sysctl.conf. You can use /etc/sysctl.conf to set all kinds of system variables. I have optimized (maybe badly) the stock Debian file over the years to look like what I show below. I address VM settings towards the last of the config. If anyone sees something I’ve done badly–Please comment.

My sysctl.conf looks like:


# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#

#kernel.domainname = example.com

# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3

##############################################################
# Functions previously found in netbase

# Increase maximum amount of memory allocated to shm
# Only uncomment if needed!
# kernel.shmmax = 67108864

# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
net.ipv4.tcp_syncookies = 1

# Increase the maximum number of skb-heads to be cached
net.core.hot_list_length = 256

# Increase number of incoming connections
net.core.somaxconn = 65000

# Increase Linux autotuning TCP buffer limits
# Set max to 16MB for 1GE and 32M (33554432) or 54M (56623104) for 10GE
# Don't set tcp_mem itself! Let the kernel scale it based on RAM.
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.rmem_default = 16777216
net.core.wmem_default = 16777216
net.core.optmem_max = 25165824

# Increase the read-buffer space allocatable
net.ipv4.tcp_rmem = 8192 87380 16777216
net.ipv4.udp_rmem_min = 16384

# Increase the write-buffer-space allocatable
net.ipv4.tcp_wmem = 8192 65536 16777216
net.ipv4.udp_wmem_min = 16384

# Make room for more TIME_WAIT sockets due to more clients,
# and allow them to be reused if we run out of sockets
# Also increase the max packet backlog
net.core.netdev_max_backlog = 2500
net.ipv4.tcp_max_syn_backlog = 20000
net.ipv4.tcp_max_tw_buckets = 2000000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 15

# don't cache ssthresh from previous connection
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.tcp_moderate_rcvbuf = 1

# How may times to retry before killing TCP connection, closed by our side
net.ipv4.tcp_orphan_retries = 1

# Protect Against TCP Time-Wait
net.ipv4.tcp_rfc1337 = 1

# Increase the maximum total buffer-space allocatable
# This is measured in units of pages (4096 bytes)
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.udp_mem = 65536 131072 262144

# Turn on the tcp_window_scaling
net.ipv4.tcp_window_scaling = 1
#
# Turn on the tcp_sack
net.ipv4.tcp_sack = 1

# Allows TCP to send "duplicate" SACKs
net.ipv4.tcp_dsack = 1

# tcp_fack should be on because of sack
net.ipv4.tcp_fack = 1
#
# Turn on the tcp_timestamps
net.ipv4.tcp_timestamps = 1

# Disable TCP slow start on idle connections
net.ipv4.tcp_slow_start_after_idle = 0

# If your servers talk UDP, also up these limits
net.ipv4.udp_rmem_min = 8192
net.ipv4.udp_wmem_min = 8192

# Disable source routing and redirects (we are not a router)
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0

# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1

# Set TCP Re-Ordering value in kernel to '5'
net.ipv4.tcp_reordering = 5

# Lower syn retry rates
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 3

# Controls IP packet forwarding
net.ipv4.ip_forward = 0
#
# increase system IP port limits
net.ipv4.ip_local_port_range = 1024 65535

# Uncomment the next line to enable packet forwarding for IPv6
#  Enabling this option disables Stateless Address Autoconfiguration
#  based on Router Advertisements for this host
#net.ipv6.conf.all.forwarding=1

# Log Martian Packets
net.ipv4.conf.all.log_martians = 1

# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1

# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1

###################################################################
#
# VM settings
vm.swappiness = 1
vm.dirty_ratio = 60
vm.dirty_background_ratio = 5
#
# Enable hard and soft link protection
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
#
# Increase system file descriptor limit
fs.file-max = 2097152
#
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
#
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1