Tuesday, 15 October 2019

Uses of virtual memory

Virtual memory seems a low level OS feature, but you can use it for solving really common patterns.

One pattern where I have been using virtual memory a lot before is for implementing a vector that doesn't need to reallocate and move all the memory when it needs to grow. Specially it is an issue if you keep a pointer somewhere to this memory.

So, instead to use std::vector, you just reserve a piece of virtual memory, that memory would be in continuous address space and it can be huge, but you only commit (ask for physical memory) for the range of addresses that you are using.

For example, you can reserve a buffer of 20 megabytes in memory but only commit 64 kb and then you can grow the committed memory if it is needed; each time that you need more memory, the memory doesn't need to be reallocated and moved, you just need to commit more memory.

Of course, there are some issues:
  • You need to know the reserve memory, but it can be huge as we are using 64 bits memory addresses and doesn't consume memory until you commit memory.
  • Virtual memory uses pages, that means that when you grow memory you are using a new page or several of physical memory; page size are different between OS/Architectures.
I use these buffers for a lot of situations in Cute; my implementation is really simple and easy to use:

Another interesting use of virtual memory is to block access inside a memory address range, that could be really useful for finding tricky bugs in you code.





No comments:

Post a Comment