
Odoo virtual memory on VM
Odoo virtual memory on VM is a critical issue. Why? Although datacnters are now thriving with VM instances, one has to keep in mind that Odoo was originally designed to be implemented on physical systems. Among the main differences, one must keep in mind that all VMs (Virtual Machines), droplets, instances and other emulated environments do not use any swap partition at all. And yet, SWAP is the guardian of stability when your environment starts falling short on memory…
As you can see, VM performanc is affected by the type of swap. Yet, absence of swap can be lethal to system stability and this is not acceptable in a production environment. Therefore, here is a simple way of adding a virtual swap partition on your VM instance. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The space on the hard drive will be used mainly when space in RAM is no longer sufficient for data. The information written to disk will be slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fall back for when your system’s RAM is depleted is a good safety net.
Creating the swap file
A proper way of getting the same file is by using the fallocate program. This command creates a file of a preallocated size instantly, without actually having to write dummy contents.
Example: we can create a 4 Gigabyte file by typing:
sudo fallocate -l 4G /swapfile
The prompt will be returned to you almost immediately. We can verify that the correct amount of space was reserved by typing:
ls -lh /swapfile -rw-r--r-- 1 root root 4.0G Apr 28 17:19 /swapfile
As you can see, our file is created with the correct amount of space set aside.
Enabling the Swap File
Right now, our file is created, but our system does not know that this is supposed to be used for swap. We need to tell our system to format this file as swap and then enable it. Before we do that though, we need to adjust the permissions on our file so that it isn’t readable by anyone besides root. Allowing other users to read or write to this file would be a huge security risk. We can lock down the permissions by typing:
sudo chmod 600 /swapfile
Verify that the file has the correct permissions by typing:
ls -lh /swapfile -rw------- 1 root root 4.0G Apr 28 17:19 /swapfile
As you can see, only the columns for the root user have the read and write flags enabled.
Now that our file is more secure, we can tell our system to set up the swap space by typing:
sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB no label, UUID=e2f1e9cf-0e16-3cc4-b3ff-624c1f6c4123
Our file is now ready to be used as a swap space. We can enable this by typing:
sudo swapon /swapfile
We can verify that the procedure was successful by checking whether our system reports swap space now:
sudo swapon -s Filename Type Size Used Priority /swapfile file 4194300 0 -1
We have a new swap file here. We can use the free utility again to corroborate our findings:
free -m total used free shared buffers cached Mem: 3953 101 3851 0 5 30 -/+ buffers/cache: 66 3887 Swap: 4095 0 4095
Our swap has been set up successfully and our operating system will begin to use it as necessary.
Make the Swap File Permanent
We have our swap file enabled, but when we reboot, the server will not automatically enable the file. We can change that though by modifying the fstab file.
Edit the file with root privileges in your text editor:
sudo nano /etc/fstab
At the bottom of the file, you need to add a line that will tell the operating system to automatically use the file you created:
/swapfile none swap sw 0 0
Save and close the file when you are finished.