We'll take a look now at managing swap space.
Currently on my Ubuntu system, I don't have any swap space,
so it's hard to monitor, isn't it?
So we're going to see how we can go through and
create and monitor our swap space.
The first thing when we use free ‑h,
it's not just showing me the memory from my RAM,
but it's also given me a summary of the swap space.
We won't have any.
If we want to look solely at our swap space, we can use swapon ‑s.
Again, the output is going to be blank, so we need to change that.
So to create our swap space, we can go ahead and use fallocate.
Now for swap space, obviously, we can use disks or partitions,
but we can also use swap files.
It's going to be easiest for us to use a swap file,
so here we're creating a swap file with a length of 500 MB.
I was about to say meters.
When we go through and look at it again,
we're running through the exactly same command,
but this time we're writing to the swap2 file of 500m,
not to meters.
We can set the permissions, so ideally, these should be set to 600.
All of this has to happen with our sudo privileges,
as does then adding the swap header.
So mkswap, often we say we are formatting the swap file system,
but there really is no file system format for swap.
We're adding header information,
so we can go through and add that to swap1 and to swap2.
So our swap devices are ready and raring for action.
When we go through and use swapon, essentially,
we're making the swap space available now.
Again,
we can look at this very similar to the mount command because
we're not mounting it through to a directory,
but it's going to be available.
And when we go through and look at swapon ‑s,
we see we now have that 500 MB available to us.
It has a priority, and the default priority starts at ‑2.
The priority controls which devices will be used for swap first.
If we have a high‑speed device,
maybe a file or a partition that's created on a solid‑state drive,
then we might want to set a higher priority,
and we can do that with a ‑p switch when we use swapon.
Here we're setting swap2 to have a priority of 10.
We're going to be able to see this, of course,
by running through the swapon ‑s again.
Another control over how much the kernel is going to swap is a
kernel parameter that we can set called swappiness.
We can go through and take a look at it in /proc/sys/vm/swappiness.
The default value is 60, but we've got a range from 0 through to 100,
where 0, we're less likely to swap memory from RAM through to our disks.
Whereas 100,
we're much more likely to be paging out to disk rather than
just dropping pages from our current memory.
Now all of this is much better not seeing it in the slides,
but seeing it in real life by working at our command line,
where we can then go through and start looking at creating our raw files,
setting permissions, adding the headers,
and then mounting our swap files with our swapon.
And a big warm welcome to the command line,
a command line where we are going to go through and
learn a little bit more about our swap.
The first thing that we're going to do is create ourselves some files,
so fallocate ‑l for length and then 500m,
and we can store it as var/swap1.
Now I need to run this with sudo.
I can move to the beginning of the line using Ctrl+A to add in the sudo.
Now that that's done, I'll _____ key and create swap2,
so we've got two new 500 MB files.
And we do need to set the permissions.
In an ideal world, these should be accessible only to the root user.
Of course, the kernel who's using the swap space will be running as root,
so we can set the permissions to 600 for both of these files,
so /var/swap*.
So that's set up our permissions,
we still need to though go through and add in our header information,
adding in text labels.
So we can use mkswp /var/swap1, and we repeat it for swap2.
As we can see that we've really set the version of swap that we're using
and a unique identifier for each of our swap spaces.
So we've got everything that we need for swap,
but these are still just files within our file system,
so we can go through and make use of the swap space
now by using sudo and swapon, and then our device.
So let's add swap1 first.
To see a summary just of our swap space, we use swapon ‑s.
I can run this as a standard user.
So we can see then that it is now available to the kernel of swap space,
but it has a priority of ‑2.
This is just the default start for our priority.
The priority is used to control the order in which the
kernel will access our swap devices.
If we're saying that swap2 is on a faster SSD device,
then we might well want to put a higher priority.
So when we go through and mount it or add the swap space in,
putting a priority of 10, ‑p 10 is certainly going to make it higher than the ‑2,
so it's going to ensure that the kernel would use this
space before using any space on swap1.
But say this could be done for our disks or our partitions,
we just happen to be using files here, but it works the same.
Another way of seeing our swap spaces from our free command, so free ‑h.
And we can see then ignoring our memory by looking at swap,
we're able to see that we've got roughly a gig of RAM available as swap,
and none of it is being used.
That's not really surprising, as so far,
we haven't really changed any of our processes.
Going back to our system activity reporter and the ‑S,
we can monitor our swap space.
Now this generally doesn't change too much over a period of time,
so we see that our values are relatively static.
However, if you look towards the bottom,
previously, I did add in some swap space,
and we can see then that we did have the gig of swap space before,
but then later it was removed.
But, of course, now that I've added the swap space back in,
we're going to see then that our swap space probably stays relatively static
as we've got no real need to use any of our swap space,
but we can then go through and monitor it using our system activity reporter.
We can go through now and take a look at our proc file system and
getting the idea of how the kernel controls the swap used.
When we go through and look in /proc/sys/vm/swappiness,
we'll be able to see that it's the default value of 60.
Now we have a range of 0 through to 100.
Where we've got a value of 0,
we're more likely to be dropping pages from RAM than using swap space.
Where we have a swappiness value of 100,
we're more likely to be using our swap space rather than
dropping pages from our system RAM where we'll be running short
of available memory for new processes.
We're going to it leave it as it is on 60.
We really don't need to make any changes.
And very often, this doesn't change very much on many systems,
but it's there, and it's available for you.
If you did need to configure it,
it's another sysctl setting that we can put in our sysctl.conf or the sysctl.d expansion directory.