In this video, you'll learn about swap partitions.
So what is swap?
Well, Linux uses swap space as emulated RAM on disk.
And that can be used to move inactive application memory from RAM to disk, and the result is
that you will have more available RAM.
Some people think that using swap is bad.
That's not so.
If only inactive application memory is swapped, performance won't be affected at all.
Because it's inactive memory, why would you want to have that in RAM?
On the other side, if active application memory is going to be involved, then you will suffer,
because disks in general are a lot slower than RAM.
Linux servers should have a minimum of swap to allow for swapping out all inactive application
memory, unless they are hosting an application that doesn't support working with swap.
You will need to read the documentation about your application to find out if that is the
case for your application.
But in general, on modern Linux servers, I would say make sure that you have 25% of RAM
available in swap.
Now if you want to allocate swap, you can create swap devices as well as swap files.
And as it works on contiguous disk space, using a swap device is recommended.
A swap file can be fragmented, and if it's fragmented, it will get slower.
If you are using swap, it's a good idea every now and then to use vmstat to monitor swap
activity, and you can use free-m to monitor swap usage.
Let me show you how you can allocate swap space.
And as an example, we are going to use a swap partition that we created using Partit in
the previous video.
If you haven't created that partition, check out the previous video to learn how to create
a swap partition.
So I'm going to use fdisk-l on dev nvme0n2, which is listing partitions, and that is verifying
that partition nvme0n2p2 is what I'm looking at.
Next I'm using free-m.
Free-m is giving an overview of current memory usage.
And you can see that I have a lot of free memory, so there's really no need to allocate
swap space, but I need to show you how it works.
So how does it work?
Well, simple, mkswap on dev nvme0n2p2.
That will format the partition as swap space.
And then I need to put it in etcfsstep.
Now what I told about uuids before applies to swap space as well, so I'm going to copy
the uuid, and I'm going to edit my etcfsstep.
And in etcfsstep, I'm adding a line uuid=" followed by the uuid that I just copied.
Oh, we already had uuid=".
Well, that means that I can remove it.
Let me do that.
Then we need to mount it.
So where are we going to mount swap space?
Well, we are going to mount swap space on none.
Just follow the example right above, which is for swap space as well.
You don't mount swap on a directory or anything.
You mount it on none, which makes it that it is going to be a kernel interface.
The type is going to be swap, and for mounting, we use defaults and 0 and 0 as additional
options.
And then swap on minus A is doing the trick.
And now when I use 3 minus M again, you can see that this system has no less than 7 gigs
of swap.
Now if ever you want to know if it is being used, then you need to run vmstat.
I'm using vmstat 2.5.
That means that it's giving me a two-second interval and five polling loops in total.
And what I'm looking at is SISO, which is giving me swap in and swap out.
This is a swap activity.
If swap activity is coming very close to BIBO, which is block in, block out, then you need
to worry about the performance.
But here, nothing is used, so we're good.