Debugging Memory Problems During Cron Run

Recently my cron stopped running completely and when I would manually visit mysite.com/cron.php I'd get a message like this:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in ...

So how do I debug this? I went to module.inc then added a bit of code to module_invoke_all();

function module_invoke_all() {
  $args = func_get_args();
  $hook = $args[0];
  unset($args[0]);
  $return = array();

  foreach (module_implements($hook) as $module) {
    $function = $module .' ' . $hook;

    // ----- Add this code ------
    if ($hook == 'cron') {
      watchdog('cron', $module . ': ' . memory_get_usage());
    }
    // --------------------------

    $result = call_user_func_array($function, $args);
    if (isset($result) && is_array($result)) {
      $return = array_merge_recursive($return, $result);
    }
    elseif (isset($result)) {
      $return[] = $result;
    }
  }
  return $return;
}

That's it. Now the next time you run your cron, take a look in your logs and you'll see exactly how much memory is being used right before (and right after) each module's hook cron. If your cron stops running in the middle, the last module to report a memory usage amount is almost certainly your culprit.

Friendly reminder: don't forget to remove this debug code once you have sorted out your memory issue.