I wounder how the RAM in LXC is managed.
If I assign 1 GB of RAM to a CT, but only 50 MB is being used. is the rest of the ram given to the host or used as cache inside the CT? Or does the system simply decides who is more worthy of the available RAM the CT or the host? If so, based on what?
It works the same way as other VMs, all the memory is managed by the host system in whatever way it normally manages memory. When you assign memory to a VM that memory isn't just allocated as a block, it is still recognised as used, free and cache by the host system and treated accordingly, setting the memory for a VM only sets the maximum amount of memory the system can use.
1 Like
So are you saying that the only OS that uses memory for caching is the host? For both CT and KVM?
Also, is this the case for both fixed allocated memory and with memory ballooning enabled? (in the case of KVM)
If it's cached memory on the VM then it's cached memory on the host system, and if it's used memory on the VM then it's used memory on the host. A container or VM or anything like that is just a process and it's memory management is treated in the same way as any other process by the host system.
This is the case for fixed memory, as I understand it ballooning makes the VM change cached memory in to free memory to free up memory on the host. But in either case used and cached memory inside the VM is seen as used and cached memory by the host.
I understand that memory is seen in the host the same way it is on the VM. used = used. cached = cached. Although what I still don't get is what are the contents of the cached memory?
Example:
A server with 8 GB of RAM.
Has 1 container running a web server. Memory allocated to the container is 7 GB. (1 GB is used, 6 GB cached.)
The 6 GB cached, as you said, is seen as cashed both in the host and in the container.
My question: are the contents of this cache are data from the web server or data from the host? is it better to give more ram to the container to cache it's own data or leave it to the host to cache the entire container's data?
If you have cached data in the VM's memory then that is the VM's data, not the hosts.
I would assume that if you would benefit from your VM caching data then it would be better to give it the memory to do so rather than leaving that memory for the host.
Thanks. One last question, do I need to dedicate some RAM to leave available to the host even though I know for a fact that my VMs will not "use" all of their RAM?
I don't think there's any issue with over allocating memory to VMs, I've done it before and didn't have any problems.