Memory Allocation Tracking

From: Gary Barnett (gbarnett@POLARNET.COM)
Date: 01/07/98

I've reached the point where it's time to start adding
statistics to the mud to track how resources are being

To that end I created a structure and filled it with
longs and updated do_show.. works fine and all..

As a side note, it was amazing the number of mob scripts
that run. 10K in 2 hours .. around 30K commands executed.

The final detail was to add some memory tracking. So.. I
started by adding a call to FREE, CREATE and str_dup that
would track the amount of memory allocated, and then subtract
the amount of memory freed.

Something like so.

#define CREATE(result, type, number)  {\
   if (!((result) = (type *) calloc ((number), sizeof(type)))) {\
            log_info("malloc failure");\
   statistics.mem_alloc+=sizeof(type)*number; }

#define FREE(item) { statistics.mem_alloc-=sizeof(item); free(item); }

As you will no doubt see, the free call is not returning the
correct amount of memory that will be freed. I deduce this from
two clues: 1) PS and the memory alloc stat get farther apart in
value as time passes  2) Creating 200 objs in a room and then purging
them does not decrease the amount of memory allocated. In fact, it goes

As the mud was up several days and the memory allocated stat
showed that there was more memory allocated than the entire
system has in both real and swap space, I can be fairly certain a
memory leak isn't the problem (or at least not significant to this

It seems that the sizeof(item) call is incorrect for the purpose.

My question:
1) What is the correct method of determining the amount of memory
a particular data structure is using? I don't want to write a routine
to handle every darn struct in the mud and all the possiblities. I'd
rather spend my time creating a new feature or finishing a skill.

2) If #1 isn't feasible, a long stored in each struct could handle the
memory allocated for the purposes of tracking, but that seems rather
odd, and less than 100% reliable, not to mention wasting 4 bytes for
every memory allocation.

3) I could create a struct to hold pointers to all the stuff that's
allocated and the amount of memory in that allocation. But that'd be
slow when it comes time to add/remove from the list.

Requirements: Just the number of bytes that are dynamically allocated
at this point in time. Fairly accurate, but not requiring attention to
OLC and other commands not needed for a production mud environment.

In particular I can live with REALLOC not being supported.

How would you do it?


Neither sweat, nor blood, nor frustration, nor lousy manuals
nor missing parts, nor wrong parts shall keep me from my task.
  --Christopher Hicks

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

This archive was generated by hypermail 2b30 : 12/15/00 PST