• Advertisement
Sign in to follow this  

Building memory tracking into an engine

This topic is 2771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm considering building memory tracking into a game engine I'm working on. I've searched google for a couple of hours, and most of what I found is related to running apps that profile other apps. I'm more interested in building it right into the engine.

What I'd like to be able to do:
1.) Know how many instances of a class have been allocated.
2.) Know exactly how much memory my program has allocated, and how much of the memory is taken up by different types of objects.
3.) Keep a log of all allocations (when it what allocated, where it was allocated from.
4.) Know about de-allocations (deletes).
5.) Know about memory leaks (objects never released).

The only helpful page I've found so far is this:
http://www.almostinfinite.com/memtrack.html

EDIT: Still looking, found this as well:
http://stackoverflow.com/questions/910172/track-c-memory-allocations

I'm curious if there are any other helpful guides out there. Thanks in advance.

[Edited by - lordikon on July 19, 2010 11:03:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
That's really cool, that's a good endeavor.

i only have some vague info for you because i've used such systems but never written my own.

* a common way to track how much memory is taken by various things is to pass a compile time defined tag to the allocation function and have it keep track of size of allocations, number etc.

* if you keep a log of all allocations as far as what lines of code allocated how much, that is potentially a lot of data and could hurt your performance, just FYI (you may already know that).

* when you implement this thing, however you do it, use macros that you can easily turn on and off with a #define. ie something like..

#ifdef MEMLOG
#define ALLOCMEM(size,tag) AllocMemTrack(size,tag)
#else
#define ALLOCMEM(size,tag) malloc(size)
#endif

then you can set it up where memlog is not defined in your final release build so that it doesnt clog it up with mem logging except for rare cases when you might be wanting to do it in release.

also you might think about spitting your data out as a csv file so that you can import it into excell and graph it etc.

Sorry i cant help you w/ more details but i hope there was something you can use in here (:

Share this post


Link to post
Share on other sites
A more OOP approach (and one that would work with classes in general) is to have a base "memory allocation" class. Something like this:


#include <map>

std::map<int, int> allocations;

class memtrack
{
private:
const int id=0;

public:
memtrack();
~memtrack();
};





memtrack::memtrack()
{
allocations[this->id]++;
}

memtrack::~memtrack()
{
allocations[this->id]--;
}




Also, I'm not sure if the STL map lets you do ++ on an uninitialized key, but it wouldn't be too much to just check if the key exists yet.

So then you could have all the objects you want to keep track of inherit from this class, and give each class a unique id. Then to find out how many objects are allocated, look up "allocations[class::id]" (you don't need an instance since id is const), and to find the memory usage do "allocations[class::id] * sizeof(class)".

Finding the total memory allocation doesn't work well with this approach however, unless you want to add up all the classes yourself, since the map has no notion of which count belongs to which class.

To log where an object was allocated, you'll have to use the preprocessor. You might do something like this:


#define newobj(xxxx, args...) new xxxx(args); printf("new instance of %s - file: %s line %s\n",#xxxx,__FILE__,__LINE__)


This is awfully ugly but it gets the job done! Note how it starts out "new xxxx" which means it's only useful for constructs of the type "class *instance = new class();" but since the other kind puts objects on the stack there's no need to keep track of them.

As far as memory leaks go, just check to see if any objects are reported as allocated as your program terminates.

Share this post


Link to post
Share on other sites
Most of this goes along with what zilchonum already said, but I'd like to add some thoughts:

1. For counting references just us a static(/global) int (not unsigned int) that gets incremented in the constructor and decremented in the destructor. It's simple to implement and gets the job done, as long as everything is single threaded.

2. I'm pretty sure new and delete can be overloaded globally, so you could insert your own
"handlers" to log the events.

3. Not sure about the windows world, but on Linux there are some pretty easy to use functions to create a stack trace (as long as compiled with debug information). Checkout backtrace() and backtrace_symbols() from #include <execinfo.h>. There should also be a name demangler around.
A stacktrace might reveal more than just the filename and line number.

4. You might be able to but the typeid operator to some good use. It can reveal an object's class name (the symbol used in the source code) during runtime.

Share this post


Link to post
Share on other sites
Quote:
Original post by lordikon
1.) Know how many instances of a class have been allocated.


Allocated or constructed? With the former, overload new/delete. For the latter some ref counting in the ctors.

Quote:
Original post by lordikon
2.) Know exactly how much memory my program has allocated, and how much of the memory is taken up by different types of objects.

use the CRT debug methods. For the types of objects allocated, that will require your own object tracking.

Quote:
Original post by lordikon
3.) Keep a log of all allocations (when it what allocated, where it was allocated from.
4.) Know about de-allocations (deletes).
5.) Know about memory leaks (objects never released).


Use CRT debug or Visual Leak Detector.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement