When you enable dynamic memory on a virtual machine – one of the things you can configure is the memory buffer:
You may be wondering what the story behind this setting is.
First – you need to understand the core mechanism behind dynamic memory. At its heart dynamic memory is all about Hyper-V calculating an “ideal target memory value” for each running virtual machine. Hyper-V then adds and removes memory in order to get virtual machines as close to their ideal target value as possible.
This “ideal target memory value” is constantly changing as the virtual machine is running. Open a new program, the virtual machine needs more memory. Close a program and it needs less. However, it would not be a good idea to actually try and keep the virtual machine exactly at the ideal target memory value for a couple of reasons:
- While dynamic memory allows Hyper-V to add memory to a virtual machine in just a few seconds (and sometimes under a second) there is still a window of time between when a virtual machine needs memory and when Hyper-V can add memory.
- Windows uses part of its memory for a file cache – which speeds up disk performance. Extra memory is needed to ensure that the best performance as achieved.
This is where the memory buffer comes into play. The memory buffer allows you to specify how much “extra” memory a virtual machine should be given when we are moving memory around between virtual machines. By default this value is set to 20% – but you can make easily make it lower or higher.
Some common questions I have heard about the memory buffer setting include:
- How does this relate to the Startup RAM and Maximum RAM settings?
Short answer – it doesn’t. A virtual machine will never go above the Maximum RAM setting or below the Startup RAM setting. The memory buffer influences how memory is assigned within these two boundaries.
- Why is this a percentage and not a megabyte value?
For fairly static workloads a megabyte value would be easier to understand. However – for virtual machines where the workloads needs change a lot – a megabyte value would not scale effectively. For example: imagine a workload that was steadily using 900MB of memory. It may be easier to say “I need a 100mb buffer” than to say “I need a 10%” buffer. So that things would look like this:
But if that virtual machine then grew to need 9GB of memory – a 10% buffer would keep 1GB free, but a 100MB buffer would only keep 100MB free:
- What is it actually a percentage of?
As I have already mentioned – the buffer is calculated as a percentage of the amount of memory that Hyper-V thinks the virtual machine needs. One thing that does trip people up is that the percentage is actually “what percentage of the total memory used by the virtual machine should be buffer”. So for a virtual machine that needed 900MB of memory – a 10% buffer would be 100MB:
Which is to say that 100MB is 10% of 1GB (which is the total amount of memory assigned to the virtual machine). Meanwhile – the same virtual machine with an 80% buffer would end up using 4.5GB of memory:
Because that is what is needed to ensure that the memory buffer is 80% of the virtual machine. For the mathematically minded what this means is that the virtual machine memory buffer setting has a logarithmic behavior.
- Why would I ever set this to a low value?
The most common reason is because you have a program running inside the virtual machine that tries to use as much memory as it can get its hands on. A low memory buffer will ensure that it does not try and grab all the memory on the physical computer.
- Why would I ever set this to a high value?
There are two reasons why you would do this. The first is because you have a workload that can benefit from having a large Windows file cache present. The second is if you have a workload that is very “spiky” (i.e. it often needs large amounts of memory quickly, but then gives up the memory just as quickly)