Skip to content
June 5, 2008 / Abe Pralle

Determining Heap Allocation Overhead

Dynamic memory allocations generally have a small, fixed amount of associated overhead that the OS uses to keep track of each allocation – 8 to 32 bytes usually.  Notably this meta-info is only necessary when the memory is freed – not when it’s in use – to determine the size of the memory that’s being freed and possibly give links to adjacent allocation blocks to aid in coalescing (defragging) unused memory.

After an embarrassingly long period of time scouring the net for info on the byte overhead for Windows OS I stumbled upon the blindingly obvious do-it-yourself solution: you just allocate two small things in a row and subtract their locations, e.g.:

  int* ptr1 = new int();
  int* ptr2 = new int();
  cout << "diff: " << ((char*)ptr2 - (char*)ptr1) << endl;
  delete ptr1;
  delete ptr2;

There’s actually still a bit of uncertainty due to alignment and round-up, but you can investigate that by trying different-sized allocations.  Basically what I turned up was that in the Cygwin environment there’s an overhead of 4-8 bytes while in Windows Vista proper there’s 24-28 bytes of overhead.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: