Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Sometimes it seems that we have nearly infinite memory resources, especially compared to the tiny 48K RAM of yesteryear’s 8-bit computers. But today’s complex applications can soak up megabytes before you know it. While it would be great if developers planned their memory management for all applications, thinking through a memory management strategy is crucial for applications with especially RAM intensive features like image/video processing, massive databases, and machine learning.
How do you plan a memory management strategy? It’s very dependent on your application and its requirements, but a good start is to work with your operating system instead of against it. That’s where memory mapping comes in. mmap
can make your application’s performance better while also improving its memory profile by letting you leverage the same virtual memory paging machinery that the OS itself relies on. Smart use of the memory mapping API (Qt, UNIX, Windows) allows you to transparently handle massive data sets, automatically paging them out of memory as needed – and it’s much better than you’re likely to manage with a roll-your-own memory management scheme.
Here’s a real-life use case of how we used mmap to optimize RAM use in QiTissue, a medical image application. This application loads, merges, manipulates, and displays highly detailed microscope images that are up to gigabytes in size. It needs to be efficient or risks running out of memory even on desktops loaded with RAM.
QiTissue highlighting tumor cell division on a digital microscopic image
The above image is stitched together from many individual microscope images, and the algorithm needs access to many large bitmaps to do that – a pretty memory-intensive process. Capturing a memory snapshot of the application in action shows that memory use grows as the application pulls in the images required for the stitching algorithm. The top purple line, Resident Set Size (RSS), is the amount of RAM that belongs to the application and that physically resides in memory. That curve reveals that memory use fluctuates but tops out over 6GB.
Memory use before mmap optimization
We used mmap extensively in our rewrite of QiTissue to help its overall memory profile. In this case, we decompress the microscope images into files and then memory map those files into memory. Note that using memory-mapped files won’t eliminate the RAM needed to load, manipulate, or display the images. However, it does allow the OS to do what it does best – intelligently manage memory so that our application operates effectively with the RAM that’s available.
Memory use after mmap optimization
The post-mmap optimization memory diagram looks similar, so what are the practical differences?
Best of all, incorporating mmap isn’t too difficult to do. Less memory, faster, and easy: what’s not to like? I’ve put together a small example on GitHub if you want to experiment around with your own mmap application: https://github.com/milianw/mmap_demo/.
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Learn Modern C++
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
Learn more
2 Comments
20 - Mar - 2019
Oleg
Hi,
Interesting idea, thanks. Actually, mapping can be used for any data known (existing) before the application starts.
But the diagrams show a small performance degradation in case of mmap. Image loading was finished before 60 seconds in case of heap and clearly after 60 seconds with mmap.
I guess it's because of additional IO operations for writing decompressed files to disk. It would be interesting to compare heap/mmap performance with uncompressed files.
20 - Mar - 2019
Milian Wolff
The performance degradation comes from the
pmap
sampling. If you compare https://github.com/milianw/mmap_demo/blob/master/mmap.txt with https://github.com/milianw/mmap_demo/blob/master/no_mmap.txt, you'll see that using mmap for this toy example is significantly faster. But when you compare these time values to the ones in the graphs, e.g. https://github.com/milianw/mmap_demo/blob/master/no_mmap.png, you'll notice that they are very different. That means: don't compare the time values in the graph, rather only concentrate on the Y-axis values there.