Hands-On High Performance Programming with Qt 5
上QQ阅读APP看书,第一时间看更新

Poor man's memory profiling

Unexpectedly, sometimes, much can be done just by using the old and trusty Process Explorer tool we introduced in the Widows system tools section earlier in this chapter. Let's discuss this with a real-life example.

In a project for one of my customers, there was a Qt application that suffered from timeouts when fetching frames from a camera. This is arguably a performance problem, so I started an investigation. As a first measure, I started a long-frame acquisition run, and Performance Graph view of Process Manager for the problematic application to monitor its behavior. You can see the measurement time series in the following screenshot:

What stands out is that the memory usage spikes and then falls rapidly. It can be observed that the timeouts occur on the falling flank of the memory usage curve. Thus, the suspicion arose that, somehow, the process of releasing memory prevents frame grabbing. A quick look into debug traces confirmed this:

AppMainWindow::outOfMemoryHandler() -> leaving out of memory handler
ImageCollector::waitForImage(): timed out !!!!

It turned out that the main window would free excess memory, blocking Qt's main message loop, so that when the ImageCollector got activated, its internal timer was already stale. Of course, this was a bug, not a real performance problem, because a grabber ran in another thread.