I’m going to perform a fresh install Kinoite on a 1TB nvme drive. I’ve configured on a test drive all the partitions/subvolumes manually using the names I want rather than the defaults.
I have the option of creating a swap file mounted at /swap. My RAM is 48Gb, so the swap would be the same (I think). Is there any value in creating this (the “automatic” settings don’t create a swap)? I shall use “sleep” preferentially to shutdown for faster boot and continuing where I left off.
Thanks,
Ric
I think the swap by default is 50% of the ram (correct me if i’m wrong), so that would be 24GB in your case.
It’s totally optional nowadays imo, on a system with that much ram it will be barely used if at all. You would be fine with a 4-8GB swap.
It’s not used for sleep/suspend, it could be used for hibernate but that is not enabled by default.
Fedora uses zram by default now: Changes/SwapOnZRAM - Fedora Project Wiki
You only need a swap partition or file if you want to be able to hibernate / suspend to disk your system.
It is considered a performance issue these days to swap to disk.
The zram swap doex ot have that issue.
The reason is that even SSDs are too slow when the kernel runs out of memory and must reclaim memory to continue.
Thanks al!
Well, that’s decided it. I have a small SSD that I use for testing and just use that system as a parallel for choosing software / trying out packages, uninstalling, etc., and for that there was no point in a swap file any way.
No swap file on the main nvme either.
Ric
Hi Folks,
Swap is used for a lot more than just hibernate
Swap is used for a lot of CopyOnWrite pages as well. Things like database engines, some games and quite a few *nix apps. When you use ZRAM as swap, you are kind of compounding the issue of running out of RAM … because ZRAM uses RAM for swap … sure, it’s faster than any disk but, that’s not the point of swap to begin with. The point of swap is to basically add virtual memory capacity to the machine in the event that processes need to be suspended, RAM freed up for other processes to execute. Also, swap is used to provide a little bit of a fail-safe in the event you really are hard on RAM and run out as it prevents oomkill from just basically killing the oldest running process in order to try and not corrupt the system by rudely killing processes ( also known as a kernel panic and subsequent complete crash) and leaving I/O writes (like flush and close a file) undone. The idea is to give the operator a chance to save themselves by noticing “OH NO!!!, I the machine is running slow and I am out of RAM and I don’t want to loose my work!!! … what do I do???”.
Answer(s):
- start a “controlled shutdown” of open apps to free RAM
- IF you have enough disk space and you need more swap (because you REALLY messed up) you can expand swap on the fly by creating a swapfile and letting swap us it while you get the system back under control.
Typical swap size pretty much depends on 1) how much RAM do you have installed 2) how likely are you to not have enough and need more virtual memory 3) how many apps do you run that need COW pages … the safe bet is to give yourself at least 4GB of swap space and then watch the system as you use it to see if you need more (top is a good way to monitor swap) then adjust up with a swapfile if needed. The other benefit of using a swap file is that you can create it, use it and destroy it pretty much at will …
Just one other quick note… IF you intend to hibernate, you are going to need pretty much the same amount of swap as you have installed RAM plus about %20 for overhead … because hibernate basically writes RAM to swap, closes all file handles and then puts the machine into a very deep sleep… run out of swap and you may as well just do a shutdown to power off and be done with it
As was mentioned earlier fedora uses ZRAM for swap.
Yes it takes some memory but the swapped pages are compressed and use much less than when actually in ram.
Speed of swap is critical to performance when swap is needed. Thus swap into ram is the fastest.
The only reason I would want physical swap would be (as mentioned) to hibernate or suspend to disk so restarting is (nearly) the same as sleeping.
If you read the docs about zram you would find that the default config for zram is 8GB, and that 8GB is shared so only used for swap when necessary.
Two commands that assist in viewing the performance of swap are
$ free
total used free shared buff/cache available
Mem: 32778200 4894828 23647872 158632 4864612 27883372
Swap: 8388604 0 8388604
$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 8G 4K 80B 12K 16 [SWAP]
Note that zramctl shows 4K data in swap which is compressed to 80B.
As long as the free command shows very little or no swap used then additional swap is not required. Only if swap is nearing 100% usage would it normally be necessary to add additional.
You may use physical swap if you wish, and change the default config of zram if you wish. For a user with 48G ram it is seldom (for most users) to ever need anything but the default zram config.
While the plethora of uses you mention are possible, the reality is that the need for extra swap beyond the default is rare, and usually depends upon the users work flow.
Only once since the introduction of zram for swap have I ever needed additional swap and that was a particular app that was processing a 32GB multimedia file.
Can you point to where this is described please?
As far as my experience goes I have not seen this need for swap when running games or other apps.
Copy-on-wrote pages do not use swap as I understand it.
Each page that is set to copy-on-write has an original page tracked by the kernel. Once a write happens the kernel allocates a new page and copies the originals contents. At no time is swap required.
Swap is only needed when the kernel runs out of memory.
I suppose my problem is two-fold: a) your answers are way ahead of my knowledge and b) there is no uniform agreement as to how I can decide (as yet).
For me the simple questions are:
- If I create a /swap partition will it take precedence over the 8Gb (default/capped) zram0 linuxswap? If it does then there will always be a performance/speed penalty hit: correct? If so, creating a /swap partition is a bad idea. Correct?
- If I create a swap partition at first install will there be any adverse effects? Or:
- will a swap partition just be benign and simply be there if ever I process large media files (which I do very occasionally) as JeffB noted?
Note: I ran free
during installling apps, compiling C++ files, etc., and at no time was zram0 ever used.
Ric
Footnote: I also read this:
– the implication was for using zswap rather than zram? Or /swap as a fallback for zram?
Normally no.
The swapon
command will show info about the defined swap and for mine, even after more than a week of normal usage shows this
$ swapon
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 8G 0B 100
Note the PRIO (priority) column. Lower numbers there mean higher priority and the default for zram is 100.
Most other swap (file, partition, etc) usually have a lower priority (higher numbers) there and would be used only after the zram was filled.
Note, however, that some have indicated problems when using both zram and physical swap, apparently caused by the difference in write speed for each.
My suggestion:
Use zram only until you have determined that additional swap may be needed. Then (and only then) determine how to manage the extra swap on different media.
Hi Barry,
Here is 1 reference and there plenty more. Also note that database engines and other apps, like the multimedia app that Jeff V (above) was using also use this area for backing-store …
Also in the old days … before ZRAMfs, we used to just build a swapfile in /tmpfs to accomplish the same thing when dsik I/O and spinning rust were an issue and the swap data was expendable …
Here is the kernel.org documentation for your reading pleasure
Swap Management
Hi Ric,
In this case, I would not use ZRAM because it is basically building a compressed ramdisk … using the exact same memory your app is going to need. Run you application, monitor RAM and swap utilization then tune accordingly another consideration is that in order to compress and decompress the data in ZRAM, your going to be using computational CPU to do so … so for CPU intensive apps … well you get the idea …
I think what you were talking about is the use of anonymous pages?
Those use swap as the backing store, but in a tuned system will not write to swap. By tuned I mean has sufficient ram to run the database, for example.
If you write one page to disk then the impact on a CPU intensive app is huge. Zram is far less impactful.
I’m in the Zram camp but nonetheless it would be interesting to see some benchmarks comparing the various approaches.
I recall seeing benchmarks on this around the time that fedora raised the change proposal for zram, but cannot track down the blog and it’s not in my notes.
Exactly!
If ZRAM is not enough then add physical swap, but otherwise do not.
The speed penalty in using physical swap for most systems in use today is enormous as compared to using zram only. Drives, including SSDs, are much slower than RAM when used for swap.
There are systems that should use large swap spaces, such as large database servers, where keeping the data in memory (the backup storage) greatly improves performance. For the typical user this is not the case.
Hi Oliver,
You can get a “quick and dirty” idea on the speed difference by simply doing this:
(In this particular case make certain you have at least 8GB of RAM installed)
- dd if=/dev/zero of=/tmp/TEST.DD bs=4096 count=1000000
- dd if=/dev/zero of=~/TEST.DD bs=4096 count=1000000
- do each about 10 times in quick succession to get a good average and , in the case of the disk, you are actually writing to the platter/main storage vs just the buffer.
#1 will give you an idea on how fast you could possibly write to RAM (/tmp is usually 50% of installed RAM as tmpfs)
#2 will give you an idea on how fast you could possibly write to the filesystem / disk
Here's is what mine looks like:
========================
iner@msige76:/tmp$ dd if=/dev/zero of=/tmp/TEST.DD bs=4096 count=1000000
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB, 3.8 GiB) copied, 0.81497 s, 5.0 GB/s
------------
einer@msige76:/tmp$ dd if=/dev/zero of=~/TEST.DD bs=4096 count=1000000
1000000+0 records in
1000000+0 records out
4096000000 bytes (4.1 GB, 3.8 GiB) copied, 3.9918 s, 1.0 GB/s
-------------
I fixed the formatting for you, hope this was ok.
Hi Barry,
Yes!!! Thank You!!!
Me and message formatting have rarely gotten along!!! LOL