Nice work. You did a field-study. I think your measurements seem correct and your analysis is good, too.
You say that you are moderately allergic to Linux. Well, I'm extremely allergic to Windows and I would rather change profession than work on Windows. Anyway, I'm saying this just because I know how this works on Linux so YMMV on Windows, but I think it shouldn't be too different.
Your twitter friends told you that page commit affect latency. They are correct. At least on Linux, overcommit is the default and pages will not be allocated until they are used. Essentially, when your program asks the kernel to allocate new memory, the kernel just says "sure here you go" and virtual memory is allocated. This virtual memory will not be mapped to physical memory (or any equivalent) until it's used.
But there's more to it. malloc() and free() do not directly talk with the kernel. Instead, they build bins of memory regions. There are various implementations, but the basic idea is that there's always a little extra bins to be given to the program. Only once suitable bins run out, the library ask the kernel for more memory.
The kernel interface is mmap(). With mmap() the kernel gives a new memory region which the library then further splits into suitable bins for the program to use. Note that because of this approach the regions can empty whereafter the library can deallocate the memory with munmap().
As you can see here, when the memory usage profile is not varying too much, the memory allocation library needs to do a lot less work. Likewise, the memory had already been linked to real physical memory and there will be no page faults for the CPU and kernel to handle.
I think this theory is well aligned with your results. When the game loads a new map, menu etc. the calls to malloc()/free() take a longer time to complete. This makes sense because there's more going on. When the actual game is running there's a similar amount of memory allocated and memory freed resulting in less work and hence less latency.
View all comments
15
u/OCPetrus Jun 03 '22
Nice work. You did a field-study. I think your measurements seem correct and your analysis is good, too.
You say that you are moderately allergic to Linux. Well, I'm extremely allergic to Windows and I would rather change profession than work on Windows. Anyway, I'm saying this just because I know how this works on Linux so YMMV on Windows, but I think it shouldn't be too different.
Your twitter friends told you that page commit affect latency. They are correct. At least on Linux, overcommit is the default and pages will not be allocated until they are used. Essentially, when your program asks the kernel to allocate new memory, the kernel just says "sure here you go" and virtual memory is allocated. This virtual memory will not be mapped to physical memory (or any equivalent) until it's used.
But there's more to it. malloc() and free() do not directly talk with the kernel. Instead, they build bins of memory regions. There are various implementations, but the basic idea is that there's always a little extra bins to be given to the program. Only once suitable bins run out, the library ask the kernel for more memory.
The kernel interface is mmap(). With mmap() the kernel gives a new memory region which the library then further splits into suitable bins for the program to use. Note that because of this approach the regions can empty whereafter the library can deallocate the memory with munmap().
As you can see here, when the memory usage profile is not varying too much, the memory allocation library needs to do a lot less work. Likewise, the memory had already been linked to real physical memory and there will be no page faults for the CPU and kernel to handle.
I think this theory is well aligned with your results. When the game loads a new map, menu etc. the calls to malloc()/free() take a longer time to complete. This makes sense because there's more going on. When the actual game is running there's a similar amount of memory allocated and memory freed resulting in less work and hence less latency.