In this article, we will discuss about Dynamic Memory in Hyper-V. We will see what the use cases are and how to configure it.
Dynamic Memory is a Hyper-V memory management features starting from Windows Server 2008 R2, it automatically reallocates the memory among the virtual machines efficiently. When enabled on a virtual machine, the memory of that virtual machine is considered as a shared resource. This means that other virtual machines can also use that memory, if required.
For example, when a virtual machine needs more memory due to performance reasons or for any other factor and if the Hyper-V host sees a virtual machine with idle memory and if dynamic memory is enabled on both the virtual machines (the one that requires more memory and also the one that has idle memory) then the host can easily adjust the amount of memory needed to the virtual machine based on the demand and available memory on the host.
This is true with almost all virtual machines running in any environment. When a virtual machine is idle their current memory usage will be very low against their startup memory. But when it is performing some job then tend to use more memory and sometimes more than the startup memory.
With the help of Dynamic Memory, you can now over-provision with respect to the memory available on the host and this means you can run more virtual machines. It is very beneficial in environment that have many idle or low-load virtual machines such as pooled VDI environments. It saves cost if you start using dynamic memory on the virtual machines.
Below are the guest operating systems that supports Dynamic Memory.
- Linux Distributions
- Windows Server 2003 SP2, Windows Server 2003 R2 SP2, Windows Vista SP1, Windows Server 2008 SP2, Windows 7, Windows Server 2008 R2, Windows Server 2008 R2 SP1, Windows 8 and Windows 8.1, Windows Server 2012 and Windows Server 2012 R2.
Note – Install the latest integration services after you set up the operating system in the virtual machine.
Let’s see how to configure Dynamic Memory on a virtual machine.
In this settings of a VM, below are the options you would see when you click on Memory.
Startup RAM
For this virtual machine, the startup RAM has been configured with 2 GB. When this VM powers on, 2 GB of RAM is reserved for this VM on the hosts, regardless of what the actual RAM demand is.
Enable Dynamic Memory
This is a checkbox where you can enable or disable dynamic memory. If the virtual machine is up and running then you need to shut down the virtual machine to enable this option. After enabling Dynamic Memory, you would be able to specify the minimum and maximum RAM for a virtual machine.
- Minimum Memory - You can specify how much of minimum memory your virtual machine need to have. In this example, it is set to 512 MB. I know we said we need 2 GB of startup memory. But in minimum memory we are saying that we just need 512 MB of RAM. The logic is that when an operating system starts up initially, it requires certain amount of memory. But when it has actually started, up and running, then it doesn’t require that much. Once the virtual machine notifies the host that its RAM requirement has decreased the host can start reducing the idle memory allocated to the virtual machine. But from the operating system’s perspective running on the virtual machine, the amount of visible RAM will never drop below the Startup RAM amount. If the virtual machine is configured with 2 GB of startup RAM then the operating system running on this virtual machine will also show 2 GB as the total RAM.
- Maximum Memory - By default, maximum memory will be set to 1 TB. But it is not recommended to leave this value set to the default. In this example, I’ve specified the maximum RAM that this VM can have is 4 GB. The good thing is that you can increase the maximum RAM while the virtual machine is running. You can now do this in Windows Server 2012 Hyper-V or later but it was not possible in Windows Server 2008 R2 Hyper-V or prior. But you cannot increase the minimum RAM when the virtual machine is running and with maximum RAM, you can increase it but you cannot decrease it when the virtual machine is running.
- Memory Buffer - Memory Buffer, by default, is set to 20%. We can always change that and by changing the value Hyper-V host will reserve 20% of configured memory of the VM as a buffer. For example, if this virtual machine is configured memory with 2 GB of startup RAM then the Hyper-V host will reserve 20% of 2 GB which is 400 MB of memory. In this example, we will leave this to default.
- Memory Weight - Memory Weight option is useful if there is memory contention among the virtual machines running on the host. With this option, you can specify which virtual machine gets more or less priority during memory contention. If I specify low as the memory weight priority for the virtual machine then it might not startup if the available memory on the host is low and other virtual machines are running and it is also used for taking memory away. The lower the weight, the most likely that machine is to lose memory when the host needs it. The higher the weight, the higher the priority when it comes time to give out memory.
Monitoring Dynamic Memory Usage
It is also important that you monitor the Dynamic Memory in Hyper-V and there are quite few ways for that.
System Center Virtual Machine Manager
SCVMM gives the most complete view of the Dynamic RAM picture.
In this image, you can see there are varying amounts of assigned RAM to the three virtual machines.
- Fastvue01 is currently demanding 2GB of RAM. The assigned amount is the demand plus the Memory buffer.
- FastVue02 is currently only demanding 1.28GB since it has a smaller workload. The assigned memory however is still 2GB because this is the specified Minimum RAM amount.
- Fastvue03 is currently only assigned 1.2GB since it has the smallest workload of all. The difference is that the Minimum RAM specified is 512MB.
The SCVMM view is great because it is complete and provides you with the total picture. It does however lag a little behind. The best place for more ‘live statistics’ are in the Hyper-V Management Console.
Hyper-V Management Console
The Hyper-V Management Console provides a simple and convenient real time view of the RAM assignment. As such, is it not a great tool to determine the minimum baseline for your virtual machines? But it can show you things like how much is your Startup Memory? Whether Dynamic Memory is enabled or not? The Minimum and Maximum Memory configured for the virtual machine, assigned memory allocated to the virtual machine by the host, the current memory demand by the virtual machine and whether the memory status is OK or not.
Performance Monitor
By using Performance Monitor (Perfmon) on the host itself, you can get a very detailed live view of the Dynamic Memory picture. The key counters to use are:
- Hyper-V Dynamic Memory Balancer - The Hyper-V Dynamic Memory Balancer counter gives you a view of how the host is doing from a memory perspective. In the image below, the host still has 4.5 of RAM available to allocate based on demand. The current demand or ‘pressure’ from the virtual machines is 80. When the pressure gets to 100, the host is essentially out of RAM. If you need to do capacity planning and would like to know how much memory is available on the host then you need to use the performance counter \\Hyper-V Dynamic Memory Balancer\Available Memory\ which determines how much memory you can safely allocate to create new virtual machines.
- Hyper-V Dynamic Memory VM - The Dynamic Memory VM counter gives you a view very similar to what we have seen from the SCVMM and Hyper-V Management consoles. One counter that is very useful here is the Guest Visible Physical Memory. This indicates how much RAM the virtual machine has visible, and it also indicates the RAM demand. Perfmon also gives us the advantage of being able to track the RAM allocation on a graph.
Monitoring Dynamic Memory Usage in Virtual Machine
I’ve a VM with Windows 8.1 on it and the startup memory is configured with 2 GB. Dynamic Memory is also enabled on the VM with minimum of 512 MB and a maximum of 4 GB.
When the VM is powered on, in the task manager, the memory usage is around 25% (0.5 GB) and in the RamMap, the unused memory is around 1.1 GB.
When the Hyper-V host sees that there is some idle memory on the host, it starts reclaiming it and the memory usage goes to 75 % and when you analyse the processes running the virtual machine, you will not be able to find any process eating up all that memory. The reason why the memory usage went high after some time from 25% to 75% is because of Dynamic Memory which has been enabled. The host saw that there is some idle memory, so it reclaimed around 1 GB of memory and locked it. If you refresh in RamMap, you can see around 1.1 GB of RAM has been locked and you can see that under Driver Locked.
Back to Task Manager, you can see that it is intelligent enough to identity the maximum memory specified for the virtual machine and because of that it shows up the maximum memory that this VM can have. Hence the startup memory is 2 GB and the maximum memory is 4 GB. Next, I’ll use a tool called TestLimit which can leak the memory you specify in MB. In command prompt, I’ll type testlimit64 –m 2048 which will leak 2 GB of memory.
As soon as I do that, you notice, in the task manager there is sudden dip with respect to memory usage, the usage comes down and it is now showing that the memory of the VM is 4 GB which is actually the maximum memory that we specified for the VM in Dynamic Memory settings. In RamMap, the driver that had locked around 1 GB of memory has been released and the unused space has become around 3 GB because we had specified 4 GB of memory in maximum memory setting of Dynamic Memory.
If I cancel the memory leak, the load will be removed, but the installed memory still reports 4 GB. This is because the visible amount of RAM will never drop below the highest allocated amount and the highest allocated amount is defined by the maximum memory value of Dynamic Memory setting for the virtual machine. If you let this virtual machine run for some time then the host will see some idle memory and it will lock it and if you go back to the task manager the memory usage will be around 75% and in the RamMap would have locked around 3 GB or memory.
No comments:
Post a Comment