Tutorial: Finding memory leaks by using PIX Timing Captures

Discover how to efficiently identify and resolve memory leaks in your game development endeavors with PIX Timing Captures.

May 14, 2024
PIX Tutorial Hero image

Imagine this scenario: you're analyzing a capture from a tester who noticed that memory use kept creeping up while playing your game. That's where PIX comes in handy. By selecting Memory checkboxes before starting a Timing Capture in PIX, you can record information about memory allocations and frees, shedding light on potential memory leaks.

Let's break down the process. We'll use three essential views in PIX: the Metrics View, the Range Details view, and the Element Details view. These views have comprehensive insights into memory use trends and allocation call stacks.

Starting with the Metrics View, we'll graph the Memory Usage metric for each allocator, such as custom allocators, HeapAlloc, and VirtualAlloc. Our goal is to find any consistent growth in memory use over time. This could indicate a memory leak.

While the graphs for custom allocators and HeapAlloc might show fluctuations in memory use in this example, the graph for VirtualAlloc tells a different story—it shows steady growth over time, signaling a potential memory leak.

Next, to pinpoint the source of the leak, we'll select a range of time where memory use is steadily increasing. Switching to the Range Details view, we'll explore the Allocation Stack Tree. It aggregates call stacks for allocations within the selected time range. Here, we'll focus on the Unfreed Size column to identify the culprit in this example—calls to VirtualAlloc.

Delving deeper into the call stack, we'll identify the specific functions that are responsible for the memory leak, such as the ResizeBuffer function that’s called from the Process function. Armed with this information, we're one step closer to resolving the issue.

Finally, we'll use the Element Details view's Source Analysis tab to go to the source code that’s responsible for the memory leak. By pinpointing the offending line of code, such as the call to ResizeBuffer, we can make corrections to get rid of the memory leak.

In conclusion, this video shows how PIX Timing Captures empower developers to find and address memory leaks efficiently, helping to ensure optimal performance for their games.