Game Development Community

dev|Pro Game Development Curriculum

Torque Memory Manager

by Pat Wilson · 11/30/2004 (9:02 am) · 5 comments

Usually, the Torque Memory Manager is something that most people run up against the first time they try to use STL. The TMM is actually a very powerful tool that can help you find memory leaks easily, track memory usage, and potentially helps improve the performance of your game.

What is it?
The TMM is a red-black tree of chunks of allocated memory which it stores in pages. By default, the TMM will allocate 8mb pages at a time. If you've ever looked at a performance graph of your running Torque game, especially one with a memory leak, you will see a line that is constant at Xmb of RAM, then it will jump up to X+8mb of RAM. The default page size of 8mb is the reason for that. To change the minimum page size, look on (or about) line 30 of 'platformMemory.cc' and you will see:
static U32 MinPageSize = 8 * 1024 * 1024;

How does it work?
This is not the place for a description of how a Red-Black tree works, Google is your friend in this case. At a higher level, though, the TMM prevents your game from allocating and un-allocating a whole bunch of memory all the time. There is an associated cost, still, with allocating/unallocating, but with the red-black tree it should be around O(log N) complexity. The TMM also stores a lot of information, with DEBUG_GUARD turned on that can help you track down memory leaks. (See my previous resource on this subject) and it can tell you where the majority of your memory is being allocated. Having a centralized location for memory allocation is also useful for tieing in your own, or 3rd party debugging tools. Without going into too much detail, Microsofts XDK has memory tracking tools which I was able to hook up to Torque very easily in order to produce more verbose dumps.

Why does it keep breaking my stuff?
The TMM redefines the new keyword and many things such as STL don't like that sort of thing. There was some work being done on getting Torque to play nice with STL, and I am not sure how that is progressing, however this is what you can do to disable the TMM in your game.

First, compile the engine with TORQUE_DISABLE_MEMORY_MANAGER defined. This will turn off the memory managing, the memory debug stuff, etc. Next, look in 'winMemory.cc' where it defines the 'new' keyword, and change it to look like this
#if !defined(TORQUE_DISABLE_MEMORY_MANAGER)
void* FN_CDECL operator new(dsize_t, void* ptr)
{
   return (ptr);
}
#else
#include <new>
#endif
That should fix your problem.

The best solution, if you are using STL, is to use Deano Calver's STL-Fix resource. It can be found Here!

#1
12/01/2004 (3:41 am)
Great! Thanks...
#2
02/02/2005 (2:56 pm)
Thanks very much for the stl_fix header. I have had a number of headaches using the TORQUE_DISABLE_MEMORY_MANAGER define with memory being overridden and crashing while trying to light levels. I have to use the stl because I am using a 3rd party text-to-voice engine in my game that relies upon it heavily and to which I only have static libs, not code.

I am still seeing the following warning; any ideas what it means?

stl_fix.h(218) : warning C4601: #pragma push_macro : 'allocator' is not currently defined as a macro
#3
02/10/2005 (6:38 pm)
In order to use TGE with a 3rd party library that uses stl and without using TORQUE_DISABLE_MEMORY_MANAGER, I still had to make a small change to winMemory.cc in addition to using the stl_fix.h header given above. This change was necessary to get things compiling in both Release and Debug modes. For anyone interested, here's the change I made:

changed from...
#ifndef TORQUE_DISABLE_MEMORY_MANAGER
void* FN_CDECL operator new(dsize_t, void* ptr)
{
   return (ptr);
}
#endif // #ifndef TORQUE_DISABLE_MEMORY_MANAGER

to...
#ifndef TORQUE_DEBUG
void* FN_CDECL operator new(dsize_t, void* ptr)
{
   return (ptr);
}
#endif // #ifndef TORQUE_DEBUG
#4
04/03/2005 (8:11 pm)
I'm trying to use the stl_fix.h, and I followed all the steps and eliminated linking errors from my own source code, but i still get a linking error from libcpmt.
I don't know how to fix this.

libcpmt.lib(locale0.obj) : error LNK2005: "void * __cdecl operator new(unsigned int,void *)" (??2@YAPAXIPAX@Z) already defined in winMemory.obj

I'd appreciate any help on this.
#5
04/26/2005 (4:52 am)
uiuc, did you solve that error?

i have to comment the new operator in winMemory.cc to get rid of the linking error in vc++ 2005. however, using vc6, it compiles even with the new operator.