Allocate swap memory to Ec2 linux instance or any other linux machine
Published: 2022-07-20 11:19:34

**- If you ever heard of the virtual memory, then in case of linux machines you are looking at swap memory.**
**- Swap space is the dedicated space from your hard disks or SSDs that is used when you Physical RAM is full.**
**- If the operating system is in need of more memory resources and the its RAM is full, inactive pages in memory are moved to the swap space. By inactive pages we mean the content that is not needed on urgent basis or not in use.**


**Before you start creating a swap file, you should check your Ubuntu system if it contains an existing swap file, use the command below to verify:**

```
swapon --show
```
**If output is empty, then you can create a new Swap file. Otherwise, If swap file is exist, then you can turn it on or else upgrade the size.**

You can also run free command to check the memory information.

```
free -h
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkzp1ubtakpubmcijuzk.png)

## How to attach a swap memory

**We can attach it to our server disk. 
Suppose we have a disk attached to our server called sdc as shown**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odobs847hfs2iq70y8yy.png)


```
fdisk /dev/sdc
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1ipj4zrdqil8ee138i2.png)

```
Type m for help
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6p0sscrq7c0sjcyl1ep.png)

```
Type 'n' :- To add a new partition
```

```
Select 'p' for default
```

**Enter partition number (1-4):1**

**Set the last sector partition size to 5G as shown below**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mtv1ikljetys79w3bqi.png)

```
Type 'p' to print partition table
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6s5y8en1khv4wpkkujfl.png)

```
Type 'l' to list know partition type
```
**As you can see the swap id hex code is 82**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7a5hnolzdsswjw5iyce.png)

```
Type 't' to change the partition
```
**Enter Hex code as 82**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idm2upaipnxwth4xh1q3.png)

**We have successfully change the partition , Type 'p' to print the Partition.**

```
Type 'w' to save and exit
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a572hf44c05l5x6ymmjx.png)

**partprobe is a program that informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table.**

```
partprobe /dev/sdc1
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kciqme2tf6gud2yzxb2b.png)

**Now use the mkswap command to setup a Swap area**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s0x3cinv1000eyvlv4l.png)

**Using swapon command, we can use the newly created space for immediate use**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2z5artoiaa5aoazj7sp5.png)

**You can now run free command to check the memory information.**

```
free -h
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5dwkultja2qwede95a4.png)

**Update the /etc/fstab file to use it at boot time only**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eedjgn8c78ejvx7sijcg.png)

**Verify the newly created swap**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkzf0p03ihd2elvacrbv.png)