1 00:00:00,000 --> 00:00:09,880 In this video, you'll learn about swap partitions. 2 00:00:09,880 --> 00:00:10,880 So what is swap? 3 00:00:10,880 --> 00:00:15,080 Well, Linux uses swap space as emulated RAM on disk. 4 00:00:15,080 --> 00:00:20,000 And that can be used to move inactive application memory from RAM to disk, and the result is 5 00:00:20,000 --> 00:00:22,420 that you will have more available RAM. 6 00:00:22,420 --> 00:00:24,760 Some people think that using swap is bad. 7 00:00:24,760 --> 00:00:25,799 That's not so. 8 00:00:25,799 --> 00:00:31,240 If only inactive application memory is swapped, performance won't be affected at all. 9 00:00:31,240 --> 00:00:35,619 Because it's inactive memory, why would you want to have that in RAM? 10 00:00:35,619 --> 00:00:41,160 On the other side, if active application memory is going to be involved, then you will suffer, 11 00:00:41,160 --> 00:00:46,040 because disks in general are a lot slower than RAM. 12 00:00:46,040 --> 00:00:50,799 Linux servers should have a minimum of swap to allow for swapping out all inactive application 13 00:00:50,799 --> 00:00:56,240 memory, unless they are hosting an application that doesn't support working with swap. 14 00:00:56,240 --> 00:01:00,080 You will need to read the documentation about your application to find out if that is the 15 00:01:00,080 --> 00:01:02,000 case for your application. 16 00:01:02,000 --> 00:01:07,720 But in general, on modern Linux servers, I would say make sure that you have 25% of RAM 17 00:01:07,720 --> 00:01:10,160 available in swap. 18 00:01:10,160 --> 00:01:16,000 Now if you want to allocate swap, you can create swap devices as well as swap files. 19 00:01:16,000 --> 00:01:20,360 And as it works on contiguous disk space, using a swap device is recommended. 20 00:01:20,400 --> 00:01:26,000 A swap file can be fragmented, and if it's fragmented, it will get slower. 21 00:01:26,000 --> 00:01:30,760 If you are using swap, it's a good idea every now and then to use vmstat to monitor swap 22 00:01:30,760 --> 00:01:36,559 activity, and you can use free-m to monitor swap usage. 23 00:01:36,559 --> 00:01:40,239 Let me show you how you can allocate swap space. 24 00:01:40,239 --> 00:01:45,559 And as an example, we are going to use a swap partition that we created using partit in 25 00:01:45,559 --> 00:01:46,919 the previous video. 26 00:01:46,919 --> 00:01:51,919 If you haven't created that partition, check out the previous video to learn how to create 27 00:01:51,919 --> 00:01:54,120 a swap partition. 28 00:01:54,120 --> 00:02:03,800 So I'm going to use fdisk-l on dev nvme0n2, which is listing partitions, and that is verifying 29 00:02:03,800 --> 00:02:08,880 that partition nvme0n2p2 is what I'm looking at. 30 00:02:08,880 --> 00:02:10,279 Next I'm using free-m. 31 00:02:10,279 --> 00:02:14,919 Free-m is giving an overview of current memory usage. 32 00:02:14,919 --> 00:02:18,880 And you can see that I have a lot of free memory, so there's really no need to allocate 33 00:02:18,880 --> 00:02:21,880 swap space, but I need to show you how it works. 34 00:02:21,880 --> 00:02:23,399 So how does it work? 35 00:02:23,399 --> 00:02:30,960 Well, simple, mkswap on dev nvme0n2p2. 36 00:02:30,960 --> 00:02:34,360 That will format the partition as swap space. 37 00:02:34,360 --> 00:02:38,399 And then I need to put it in etcfsstep. 38 00:02:38,399 --> 00:02:44,160 Now what I told about uuids before applies to swap space as well, so I'm going to copy 39 00:02:44,399 --> 00:02:49,119 the uuid, and I'm going to edit my etcfsstep. 40 00:02:49,119 --> 00:02:57,960 And in etcfsstep, I'm adding a line uuid=" followed by the uuid that I just copied. 41 00:02:57,960 --> 00:03:00,160 Oh, we already had uuid=". 42 00:03:00,160 --> 00:03:05,839 Well, that means that I can remove it. 43 00:03:05,839 --> 00:03:11,479 Let me do that. 44 00:03:11,479 --> 00:03:13,059 Then we need to mount it. 45 00:03:13,059 --> 00:03:16,580 So where are we going to mount swap space? 46 00:03:16,580 --> 00:03:20,179 Well, we are going to mount swap space on none. 47 00:03:20,179 --> 00:03:24,580 Just follow the example right above, which is for swap space as well. 48 00:03:24,580 --> 00:03:27,419 You don't mount swap on a directory or anything. 49 00:03:27,419 --> 00:03:32,380 You mount it on none, which makes it that it is going to be a kernel interface. 50 00:03:32,380 --> 00:03:40,179 The type is going to be swap, and for mounting, we use defaults and 0 and 0 as additional 51 00:03:40,179 --> 00:03:41,179 options. 52 00:03:41,179 --> 00:03:45,460 And then swap on minus A is doing the trick. 53 00:03:45,460 --> 00:03:51,380 And now when I use 3 minus M again, you can see that this system has no less than 7 gigs 54 00:03:51,380 --> 00:03:52,940 of swap. 55 00:03:52,940 --> 00:03:59,619 Now if ever you want to know if it is being used, then you need to run vmstat. 56 00:03:59,619 --> 00:04:02,300 I'm using vmstat 2.5. 57 00:04:02,300 --> 00:04:07,580 That means that it's giving me a two-second interval and five polling loops in total. 58 00:04:07,580 --> 00:04:12,699 And what I'm looking at is SISO, which is giving me swap in and swap out. 59 00:04:12,699 --> 00:04:14,500 This is a swap activity. 60 00:04:14,500 --> 00:04:21,380 If swap activity is coming very close to BIBO, which is block in, block out, then you need 61 00:04:21,380 --> 00:04:23,779 to worry about the performance. 62 00:04:23,779 --> 00:04:26,619 But here, nothing is used, so we're good.