Memory allocation

Discussions related to using VirtualBox on Windows hosts.
Post Reply
Lars78
Posts: 5
Joined: 3. Dec 2013, 15:40

Memory allocation

Post by Lars78 »

Hello folks.

We have observed in a one or two rare cases C++ "std::bad_alloc" in our software. This does not happen when running on the host.

To investigate this issue I am trying to understand memory allocation in Virtual Box and any limitations.

I have the following setup:
Host: Windows 10 Build 19044 with 16 logical processes and 32 GB RAM
Guest: Windows 10 Build 18362

We have a 64-bit test application that allocates blocks of of memory on the heap in a loop. User can decide size of memory block and how many blocks to create. Each byte in memory block is assigned random value.

* Case #1 - Running the guest with 16 GB base memory.
- Execute test application to create 30 GB of data in blocks of 1 GB. The test application is able to allocated 25,3 GB of virtual memory (22 GB private memory and 14,8 GB working memory) before throwing a std::bad_alloc exception.

* Case #2 - Running the guest with 16 GB base memory.
- First execute test application to create 4 GB of data in blocks of 1 GB (without any exception)
- Secondly execute test application to create 30 GB of data in blocks of 1 GB. The test application is able to allocated 22,1 GB of virtual memory (17,8 GB private memory and 12,1 GB working memory) before throwing a std::bad_alloc exception.

* Case #3 - Running the guest with 4 GB base memory.
- Execute test application to create 30 GB of data in blocks of 1 GB. The test application is able to allocated 13,7 GB of virtual memory (9,4 GB private memory and 3,1 GB working memory) before throwing a std::bad_alloc exception.


Questions
1. What is the relationship between "base memory" value and how much memory a process can allocated in the guest OS? Base memory of 4 GB appear to allow a process to allocated 13 GB. Quadrupling the base memory will "only" allowed a process to allocated 25 GB.
2. Comparing #1 and #2 there appears to be a limit to how much memory the OS can allocated. Is that correct? How can I determine this limit?
3. Other posts mention that Virtual Box uses MmAllocateContiguousMemory API. What is the relationship between the value used in this API and "base memory"? How can I determine which value is used in the API?
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Memory allocation

Post by scottgus1 »

I can't speak to most of what you have there, but I have learned that Virtualbox allocates RAM to the VM in a "lazy" fashion, giving the RAM when the VM asks for it.

There is a command to turn this behavior off and give all the RAM to the VM at once:

VBoxManage setextradata "VM name" "VBoxInternal/RamPreAlloc" 1

Maybe this will stop the errors?
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Memory allocation

Post by fth0 »

Lars78 wrote:1. What is the relationship between "base memory" value and how much memory a process can allocated in the guest OS?
The Base Memory of a VM is equivalent to the physical memory of a PC. How much memory a process running in the guest OS can allocate, depends on the memory managements of the guest OS and the host OS. As long as the host OS has plenty of unused memory, you can investigate on the guest OS alone.
Lars78 wrote:2. Comparing #1 and #2 there appears to be a limit to how much memory the OS can allocated. Is that correct? How can I determine this limit?
The memory management of a modern OS (e.g. Windows 10) is quite a complex beast and constantly changing even on a seemingly idle PC. Using virtualization you have two of them, the physical PC and the VM. You can only determine the current limit by experimentation, but I'm not sure if that makes sense. A decent program should always be able to cope with getting no more memory, so it may be better to handle the exception in the program.
Lars78 wrote:3. Other posts mention that Virtual Box uses MmAllocateContiguousMemory API. What is the relationship between the value used in this API and "base memory"? How can I determine which value is used in the API?
If you got this information from a lengthy post of mine in the past, note that we were discussing VirtualBox 5.2.44 on a 32-bit Windows 7 host OS back then. On your Windows 10 host, VirtualBox probably uses MmAllocatePagesForMdlEx() and MmAllocatePagesForMdl() to allocate chunks of 2 MB whenever necessary.

As a general hint, first check if VirtualBox is running under the Hyper-V hypervisor on your Windows host, by looking for a green turtle in the status line of your VM's window. If you see the green turtle, VirtualBox doesn't use its own memory management, but the Hyper-V one.
Lars78
Posts: 5
Joined: 3. Dec 2013, 15:40

Re: Memory allocation

Post by Lars78 »

Appreciate your help :-)

Forgot to mention that we are using Virtual Box 6.1.36.
fth0 wrote: The Base Memory of a VM is equivalent to the physical memory of a PC. How much memory a process running in the guest OS can allocate, depends on the memory managements of the guest OS and the host OS. As long as the host OS has plenty of unused memory, you can investigate on the guest OS alone.
So in #3 the VM is allocated 4 GB of physical memory and I am still able to allocated roughly 13 GB of memory in a process running on the guest OS. How is this possible? Does this mean the guest OS does not store all the data in physical memory? Does the guest OS store some of the data in a page file?

fth0 wrote: The memory management of a modern OS (e.g. Windows 10) is quite a complex beast and constantly changing even on a seemingly idle PC. Using virtualization you have two of them, the physical PC and the VM. You can only determine the current limit by experimentation, but I'm not sure if that makes sense. A decent program should always be able to cope with getting no more memory, so it may be better to handle the exception in the program.
Agree with all of that.

Say you have a software solution installed on a VM that consist of running multiple application that are both external and internal with various hardware needs. Then say you in some rare cases see issues with memory allocations. In this situation it would be beneficial to have some knowledge of the guest OS limitations.

Does Virtual Box provide any logging which would tell me that it cannot allocate any more memory? This would help us determine if a bad allocation happened because of incorrectly configured VM and not because of a corrupt heap in one of our application.
fth0 wrote: If you got this information from a lengthy post of mine in the past, note that we were discussing VirtualBox 5.2.44 on a 32-bit Windows 7 host OS back then. On your Windows 10 host, VirtualBox probably uses MmAllocatePagesForMdlEx() and MmAllocatePagesForMdl() to allocate chunks of 2 MB whenever necessary.

As a general hint, first check if VirtualBox is running under the Hyper-V hypervisor on your Windows host, by looking for a green turtle in the status line of your VM's window. If you see the green turtle, VirtualBox doesn't use its own memory management, but the Hyper-V one.
Yes, I was referring to your post :-)

To my surprise I am using Hyper-V.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Memory allocation

Post by fth0 »

Lars78 wrote:So in #3 the VM is allocated 4 GB of physical memory and I am still able to allocated roughly 13 GB of memory in a process running on the guest OS. How is this possible? Does this mean the guest OS does not store all the data in physical memory? Does the guest OS store some of the data in a page file?
Yes, and yes.

Your questions indicate that your knowledge about the Windows memory management could need some improvement. ;) I'd suggest to start by reading Pushing the Limits of Windows: Physical Memory and Pushing the Limits of Windows: Virtual Memory. Afterwards or alternatively, search YouTube for the two-part series "Mysteries of Memory Management Revealed" by Mark Russinovich.
Lars78 wrote:Does Virtual Box provide any logging which would tell me that it cannot allocate any more memory?
Yes, but the VM would usually be aborted in that case. Start the VM from the powered-off state, reproduce the issue and provide a (zipped) VBox.log file, and I'll have a look.
Post Reply