By default, LXD Virtual Machines do not preallocate their memory up to the limits.memory
set. Instead, they allocate up to the maximum memory has needed during its lifetime, but they do not give back memory to the host system after it is freed, ballooning devices notwithstanding.
So, because the memory isn't given back to the host, why not preallocate the memory used by the VM? This way it is easier to reason about if you have enough memory in your system, instead of trying to think about "uuhh I do have 10240 megabytes free, but one of my VMs has a 6144 megabytes limit, and currently they are using 2048 megabytes, so actually we have 6144 megabytes free".
First, you need to shutdown your virtual machine...
lxc stop test-memoryalloc
Now, if you haven't already, let's set the memory limit for the virtual machine...
lxc config set test-memoryalloc limits.memory 8GB
And then we add QEMU's -mem-prealloc
parameter to the VM!
lxc config set test-memoryalloc raw.qemu "\-mem-prealloc"
That's it! If you check how much memory the QEMU process for the test-memoryalloc
virtual machine is using, you'll see that it will be using ~8GB.
And yes, you need to add the \
before the dash, because if you don't, LXD will complain about Error: unknown shorthand flag: 'm' in -mem-prealloc
.
If you want to remove the -mem-prealloc
parameter, you can remove it by using lxc config unset
.
lxc config unset test-memoryalloc raw.qemu