Tracking Memory Allocations

Inspired by a series of articles by Jelle van der Beek ([1-3]), as well as the implementation by Maciej Sinilo ([4]), we’ve decided to write an application for tracking memory usage and statistics of unmanaged applications. Its primary use would be to monitor the engine’s memory requirements and tracking down memory leaks.

The prototype is based on the idea of having a DLL (the client) staticaly linked to each and every module you want to monitor (ala Visual Leak Detector). The client intercepts all memory related functions (malloc/realloc/free, as well as scalar and vector versions of new and delete, both from msvcrXX.dll and mfcXX.dll), and sends messages to a server application through RPCs-over-tcp. The RPCs are synchronous at the moment, but since they don’t do a lot of work it shouldn’t be a big problem. Processing of all messages is done by the server, outside of the RPCs, in order to keep the overhead to the minimum.

Following the useful tips given in [5], the real-time view of the server displays only statistics about the current state of the monitored application (see figures below). Actual processing of all messages is performed on demand, from the other views of the application. Currently, the server takes up to about 2 – 4% of CPU time, on a Q6600, when displaying the real-time view (which is updated constantly). The main problem right now is the required network bandwidth to transfer callstack information from the client to the server, because we aren’t compressing the callstack in any way (except from limiting the number of frames). This might actually appear as a big problem when the server is running on a different PC, which would require actual data transfer over a wire.

Real-time statistics
Real-time view of the server application. Displays statistics for the current state of the monitored application.


Active allocations
Active Allocations: This tab displays all the active allocations. Updates are performed on demand. Useful for finding out memory leaks (after the monitored application closes) as well as tracking down memory usage per function.


Memory fragmentation
Fragmentation: Displays the heap structure at any given time. Clicking on any of the blocks in the graph, displays the callstack of the allocated object.


Allocation size histogram
Histogram: Displays the number of allocations (active or total), grouped in buckets of power-of-two.



The project is still in alpha version, and there are a lot of things to be tested and added. I.e. currently, the user has the ability to save all the information to a file, at any given time, and load it later for inspection. It would be nice if there was a mechanism of easiliy comparing two dumps, and finding differences between them (e.g. track the memory allocated by a specific action in the monitored application). Other things we are currently planning (in no specific order):

  • Addition of another tab, for finding the “critical path” of your application, like “CallGraph analysis” view from [1].
  • An API for communicating between the monitored application and the server. This way, the user would be able to add markers inside the code (e.g. FrameStart and FrameEnd) in order to track down the memory used in a specific frame, or the subsystem which made the allocations (like in [6]).
  • Detection of buffer overruns, by adding guard bytes at the end of every allocation

If you find such application useful, we’ll consider releasing a demo/beta version in the future. But first we must fix some minor but annoying bugs.

Thanks for reading. As always, comments are welcome.

References:
[1] van der Beek, J., Monitoring Your PC’s Memory Usage For Game Development”
[2] van der Beek, J., Monitoring Your Console’s Memory Usage, Part One
[3] van der Beek, J., Monitoring Your Console’s Memory Usage, Part Two
[4] Sinilo, M., Posts about MemTracer at .mischief.mayhem.soap.
[5] Sinilo, M., MemTracer strikes back
[6] Lewis, M., Managing Your Memory – Part III : Tracking and Reporting Allocation Information

Tags:

4 Responses to “Tracking Memory Allocations”

  1. Greg Says:

    do you plan to make this tool available please?

  2. Jelle van der Beek Says:

    Hey guys… don’t know if this thread is still active, but I posted a video of my new memory tool on Youtube. Check it out!

    http://www.youtube.com/watch?v=T4fEijatabk

  3. JD Says:

    Sorry for the late approval but Akismet blocked the comment because of the 2 links.

    That seems like a great tool. It’s nice to see the progress since your Gamasutra articles. We are still working on and off on our tool but we haven’t had the chance to post an update since our last post (one year ago).

    We’ll be watching your youtube channel for updates on this tool. If there is a demo somewhere I’ll also be glad to give it a try.

    Thanks for stopping by.

  4. Jelle van der Beek Says:

    Alright! let me know if there’s any news on your tools. Keep up the good work!

Leave a Reply


nine − = 7