Latest Posts

Change SWAP Size to Improve Low Memory Cloud VM Performance

I am having a couple of low memory vm in the cloud to run some basic testing lab. It always gets me a notification for the performance , especially on memory since they are only have 587M memory.

Performance Issue on Low Memory Cloud VM







Change SWAP File Size

Linux provides for two types of swap space. By default, most Linux installations create a swap partition, but it is also possible to use a specially configured file as a swap file. A swap partition is just what its name implies—a standard disk partition that is designated as swap space by the mkswap command.  If you don’t have enough memory, swap will be used quite often and noticeably more during any memory requirement spikes. If you don’t have enough memory and no swap space, this will often cause failure to allocate memory for requests needing more memory pages. As a last resort, the Kernel will deploy OOM killer to nuke high-memory processes (usually MySQL, java, etc), which shows in my previous post - Linux Virtual Machine PHP Invoked OOM-killer (Out of Memory) Issue.

Here comes recommended SWAP spaces:
Amount of system RAM
Recommended swap space
Recommended swap with hibernation
less than 2 GB
2 times the amount of RAM
3 times the amount of RAM
2 GB - 8 GB
Equal to the amount of RAM
2 times the amount of RAM
8 GB - 64 GB
0.5 times the amount of RAM
1.5 times the amount of RAM
more than 64 GB
workload dependent
hibernation not recommended

In my case, my VM is only having 587M memory with 0K swap file size.



Based on recommendation, I am adding a 1GB swap file for it.
[[email protected] ~]# sudo fallocate -l 1G /swapfile
[[email protected] ~]# sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 27.5052 s, 39.0 MB/s
[[email protected] ~]# sudo chmod 600 /swapfile
[[email protected] ~]# sudo mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=d126e13f-162d-4a1d-998c-f8e4152e4f8b
[[email protected] ~]# sudo swapon /swapfile
[[email protected] ~]# vi /etc/fstab
[[email protected] ~]# mount -a
Add a new line into /etc/fstab file:
/swapfile swap swap defaults 0 0

Notes: How do I allocate memory to work as swap space in an Amazon EC2 instance by using a swap file?

Verification by using commands:
  • top
  • htop
  • free -h




To Sum up all commands again:

  1. sudo fallocate -l 1G /swapfile
  2. sudo chmod 600 /swapfile
  3. sudo mkswap /swapfile
  4. sudo swapon /swapfile
  5. echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab


Performance Testing

With the methods posted in my post : VPS Bench Comparison for AWS,Azure,Google Cloud and Oracle Cloud (Free Tier & Regular Tier)

I could not tell much difference from before and after swap increase change. It might be will tell difference when there are some heavy application using memory.

Before SWAP Increase
After SWAP Increase
nench.sh v2019.07.20 -- https://git.io/nench.sh
 benchmark timestamp:    2020-03-18 21:17:43 UTC
-------------------------------------------------

Processor:    Intel(R) Xeon(R) CPU @ 2.30GHz
CPU cores:    1
Frequency:    2300.000 MHz
RAM:          587M
Swap:         -
Kernel:       Linux 3.10.0-1062.12.1.el7.x86_64 x86_64

Disks:
sda     10G  HDD

CPU: SHA256-hashing 500 MB
    1.719 seconds
CPU: bzip2-compressing 500 MB
    CPU: AES-encrypting 500 MB
    1.052 seconds

ioping: seek rate
    min/avg/max/mdev = 452.8 us / 5.33 ms / 51.9 ms / 8.48 ms
ioping: sequential read speed
    generated 2.46 k requests in 5.00 s, 614 MiB, 490 iops, 122.7 MiB/s

dd: sequential write speed
    1st run:    35.86 MiB/s
    2nd run:    36.05 MiB/s
    3rd run:    36.05 MiB/s
    average:    35.99 MiB/s

IPv4 speedtests
    your IPv4:    35.226.94.xxxx

    Cachefly CDN:         110.45 MiB/s
    Leaseweb (NL):        17.40 MiB/s
    Softlayer DAL (US):   60.76 MiB/s
    Online.net (FR):      17.77 MiB/s
    OVH BHS (CA):         44.79 MiB/s

No IPv6 connectivity detected
-------------------------------------------------
nench.sh v2019.07.20 -- https://git.io/nench.sh
 benchmark timestamp:    2020-03-18 21:36:09 UTC
-------------------------------------------------

Processor:    Intel(R) Xeon(R) CPU @ 2.30GHz
CPU cores:    1
Frequency:    2300.000 MHz
RAM:          587M
Swap:         1.0G
Kernel:       Linux 3.10.0-1062.12.1.el7.x86_64 x86_64

Disks:
sda     10G  HDD

CPU: SHA256-hashing 500 MB
    1.731 seconds
CPU: bzip2-compressing 500 MB
    CPU: AES-encrypting 500 MB
    1.059 seconds

ioping: seek rate
    min/avg/max/mdev = 338.6 us / 4.13 ms / 68.9 ms / 6.66 ms
ioping: sequential read speed
    generated 2.46 k requests in 5.00 s, 615.5 MiB, 492 iops, 123.1 MiB/s

dd: sequential write speed
    1st run:    35.86 MiB/s
    2nd run:    36.05 MiB/s
    3rd run:    36.05 MiB/s
    average:    35.99 MiB/s

IPv4 speedtests
    your IPv4:    35.226.94.xxxx

    Cachefly CDN:         101.90 MiB/s
    Leaseweb (NL):        17.43 MiB/s
    Softlayer DAL (US):   60.28 MiB/s
    Online.net (FR):      17.80 MiB/s
    OVH BHS (CA):         32.00 MiB/s

No IPv6 connectivity detected
-------------------------------------------------


References









No comments