tl;dr: You should read this document before writing a memory reporter. And please ask nnethercote to co-review any memory reporter patch.

Mozilla code has infrastructure that lets different parts of the code report on their memory usage. This is most obviously used in about:memory and telemetry. This document describes things that you should know when writing a memory reporter.

Memory Reporters

A memory reporter makes one or more memory measurements (a.k.a. reports).

Each reporter implements a collectReports function which takes a nsIMemoryReporterCallback argument; for each measurement the reporter must pass in several values, including:

See the nsIMemoryReporter documentation and nsIMemoryReporter.idl for full details.

Making Measurements

nsIMemoryReporter provides the high-level interface for a memory reporter, but the heart of a memory reporter is the measurement of the "amount".

Two Ways to Measure

Memory reporters can be divided into the two following kinds.

Traversal-based reporters are preferable, for the following reasons.

Sometimes counter-based reporters are unavoidable, particularly when writing memory reporters for third-party code that cannot be modified.

A Simple Example

Imagine a simple string class with the following data fields:

class MyString
{
  private:
    char *mBuffer;    // heap-allocated
    size_t mLen;

  // ... methods ...
}

Here are what the measurement functions (yes, functions) should look like for this class.

size_t MyString::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
  return aMallocSizeOf(mBuffer);
}
size_t MyString::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
  return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}

(Note that SizeOfExcludingThis and SizeOfIncludingThis aren't overrides of methods on a global base class that is common to all reporters. These names are just a convention that is commonly followed. That said, note that for some classes these methods may be virtual.)

mfbt/MemoryReporting.h defines mozilla::MallocSizeOf as follows:

typedef size_t (*MallocSizeOf)(const void* p);

Functions with this signature measure the size of p by asking the heap allocator how big it is (via moz_malloc_usable_size).

All this is probably not what you'd expect, but the above functions have the following crucial features.

Some other things to note.

And here's how you'd write a memory reporter if there was a single global MyString object.

MyString *gMyString;

class MyStringReporter MOZ_FINAL : public nsIMemoryReporter
{
  MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)

public:
  NS_DECL_ISUPPORTS

  NS_METHOD
  CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData)
  {
    // BTW: If gMyString wasn't a pointer, you'd use
    // |gMyString.SizeOfExcludingThis(MallocSizeOf)| instead.
    return MOZ_COLLECT_REPORT(
      "explicit/mystring", KIND_HEAP, UNITS_BYTES,
      gMyString->SizeOfIncludingThis(MallocSizeOf),
      "Memory used for MyString.");
  }
};

NS_IMPL_ISUPPORTS1(MyStringReporter, nsIMemoryReporter)

Note that MOZ_DEFINE_MALLOC_SIZE_OF defines a function of type mozilla::MallocSizeOf that is specific to this memory reporter (and will be identified as such in DMD's output). And MOZ_COLLECT_REPORT is a macro that makes things a bit shorter.

An Example Involving Inheritance

Things are a little trickier when inheritance is involved. An example:

class B
{
  virtual foo() { ... }

  virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
  {
    return ... // measure things pointed to by B-specific fields
  }

  virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
  {
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  }

  // data members
};

class D : public B
{
  virtual foo() { ... }

  virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const override
  {
    size_t n = B::SizeOfExcludingThis(aMallocSizeOf);
    n += ...  // measure things pointed to by D-specific fields
    return n;
  }

  virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const override
  {
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  }

  // data members
};

Things to note about SizeOfExcludingThis when it is virtual.

Things to note about SizeOfIncludingThis when it is virtual.

Other Considerations

A number of the existing basic data structures already have SizeOf{In,Ex}cludingThis functions, e.g. nsTArray and nsTHashtable. nsTHashtable's functions take an extra argument which is a pointer to a function that measures the memory usage of any structures pointed to by entries in the hash table.

Sometimes you may need variations on the above forms. For example, if you have a function that just measures one member mFoo of an object, it might be called SizeOfFoo. Try to make the names descriptive enough that it's clear what's being measured.

Sometimes you might want to split the measurements of an object into two or more numbers, e.g. because you want to show them separately in about:memory. In this case it's often clearer to increment the numbers rather than assigning to them, especially if you're measuring multiple entities and summing their measurements. For example:

void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
                                    size_t *aFooSizeOut, size_t* aBarSizeOut) const
 {
     *aFooSizeOut += ...;
     *aBarSizeOut += ...;
 }

Alternatively, you could create a struct:

struct FooBarSizes
{
  size_t mFoo;
  size_t mBar;
  FooBarSizes() { mozilla::PodZero(this); }
}

void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
                                    FooBarSizes* aSizes) const
{
  aSizes->mFoo += ...;
  aSizes->mBar += ...;
}

Note the Add prefix that makes this incrementing behaviour clear. Obviously, if you increment you have to zero the values at some point. When using a struct, its constructor is the obvious place for this.

You could even put the nsMallocSizeOfFun in FooBarSizes to reduce the number of arguments.

Sometimes it's difficult and/or not worth measuring every member of an object of in a Foo::SizeOfExcludingThis method. This is ok, but it's worth adding a comment explaining this to the method.

Occasionally you do want to compute sizes analytically instead of using moz_malloc_size_of. In that case, use ComputedSizeOf as the prefix for the function name.

It's important that no memory is measured twice; this can lead to strange results in about:memory. Avoiding double measurement is easy in tree-like structures. In graph-like structures (where an object might be pointed to by more than one other object) it gets more difficult, and might even require some way to mark objects that have been counted (and then a way to unmark them once the measurement is complete).

It's easy to get confused as to whether you should use SizeOfIncludingThis or SizeOfExcludingThis. A good rule of thumb: if you're calling the method via a pointer, you usually want the former, otherwise you want the latter. For example:

foo->SizeOfIncludingThis()  // yes
foo.SizeOfExcludingThis()   // yes

foo.SizeOfIncludingThis()   // no
foo->SizeOfExcludingThis()  // no

Sometimes memory reporters are stand-alone objects, like the MyStringReporter example above. But often you'll have a singleton object that needs measuring, in which case it is usually better not to have a separate reporter object, but instead for the singleton object to implement nsIMemoryReporter, and thus measure and report its own memory consumption. There are many such examples in the code, such as nsCategoryManager.

If you write a memory reporter, please get two people to review it: (a) someone who knows the data structures being measured, and (b) nnethercote, who can check for all the things covered by this document. Thanks!

DMD

DMD is a tool that detects two deficiencies with memory reporters.

DMD is absolutely crucial; these things cannot be done without it. That's why the integration of DMD and memory reporters is so important and thus mentioned multiple times above.

See the instructions on how to run DMD.