The main reason of why vagrant images uses that amount of RAM is because they don’t have SWAP, so, if your program/script is using more RAM than what is in the VM the program/script is killed.
You can add a SWAP file by using dd
$ dd if=/dev/zero of=/swapfile bs=1024 count=655360 $ chmod 00600 /swapfile $ swapon -a
Then add it to /etc/fstab
. Pretty simple, but you have a big file that may or may not be used in totality.
A better approach is to use swapspace
daemon, it will create a swap space that will grow/shrink as needed. You can install the service in your VM if you already have it working or, add this to your Vagrantfile
to have it done automagically.
config.vm.provision "shell", inline: "sudo apt install swapspace -y"
If you do this with a VM already working just shut it down, use vagrant up --provision
to run the provision instruction and you are done!.
Having a Swap space allows you to reduce the amount of RAM dedicated to your VM.