Improving Performance on Low-Memory Linux VMs


Low-memory Linux virtual machines (less than 4GB RAM) can be cost-effective means for running web applications or hosting websites. However, sometimes performance can be constrained on such VMs. Presented below are a few suggestions for maximizing performance on low-memory Linux VMs.

Kernel tuning

We will use the ´sysctl´ program to tune a couple of Linux kernel parameters. First, we'll set the swappiness) of the kernel. As the root user, run:

sysctl -w vm.swappiness=10

This setting will make the kernel prefer to keep application memory pages in RAM rather than paging them to the swap space. If memory gets really low, the kernel will page running applications to swap, but only as a last resort. This should keep as many running applications responsive as possible. To make this change permanent, either create a file named ´30-vm-swappiness.conf´ in your /etc/sysctl.d/ directory containing the following line or add this line you your /etc/sysctl.conf file:

vm.swappiness = 10

Next, let's change the vfs_cache_pressure setting. Again, as the root user, run:

sysctl -w vm.vfs_cache_pressure=50

This setting will make the kernel somewhat more aggressive in reclaiming RAM from the disk and swap caches, freeing that memory to be used by running applications. To make this change permanent, either create a file named 30-vm-vfs_cache_pressure.conf in your /etc/sysctl.d/ directory containing the following line or add this line you your /etc/sysctl.conf file:

vm.vfs_cache_pressure = 50


MySQL/MariaDB tuning

MySQL/MariaDB is a very commonly-used DBMS in web applications. If you are using MySQL/MariaDB, you can customize your /etc/my.cnf file to tune some parameters. In particular, pay attention to the key_buffer, query_cache_limit, query_cache_size, and query_cache_type settings. I can't give you specific values, but the following is a reasonable set of defaults for a 512MB Linux virtual machine:


[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
query_cache_limit = 1M
query_cache_type = 1
query_cache_size = 64M

 

Conclusion

I've focused on two of the easiest tweaks you can make for improving the performance of a low-memory Linux virtual machine. There are many other configuration tweaks. You should look at the configuration options of your web server (Apache, Nginx) as well. If you're using PHP, examine those configuration options as well. With a little time and effort, you can get a lot of bang for the buck using low-memory Linux virtual machines.

Posted in
Last update:
2016-02-29 10:17
Average rating:0 (0 Votes)