def _SubtractMemoryStats(end_memory_stats, start_memory_stats):
  """Computes the difference in memory usage stats.

  Each of the two stats arguments is a dict with the following format:
      {'Browser': {metric: value, ...},
       'Renderer': {metric: value, ...},
       'Gpu': {metric: value, ...},
       'ProcessCount': value,
       etc
      }
  The metrics can be VM, WorkingSetSize, ProportionalSetSize, etc depending on
  the platform/test.

  NOTE: The only metrics that are not subtracted from original are the *Peak*
  memory values.

  Returns:
    A dict of process type names (Browser, Renderer, etc.) to memory usage
    metrics between the end collected stats and the start collected stats.
  """
  memory_stats = {}
  end_memory_stats = end_memory_stats or {}
  start_memory_stats = start_memory_stats or {}

  for process_type in end_memory_stats:
    memory_stats[process_type] = {}
    end_process_memory = end_memory_stats[process_type]
    if not end_process_memory:
      continue

    # If a process has end stats without start stats then report the end stats.
    # For example, a GPU process that started just after media playback.
    if (process_type not in start_memory_stats or
        not start_memory_stats[process_type]):
      memory_stats[process_type] = end_process_memory
      continue

    if not isinstance(end_process_memory, dict):
      start_value = start_memory_stats[process_type] or 0
      memory_stats[process_type] = end_process_memory - start_value
    else:
      for metric in end_process_memory:
        end_value = end_process_memory[metric]
        start_value = start_memory_stats[process_type].get(metric, 0)
        if 'Peak' in metric:
          memory_stats[process_type][metric] = end_value
        else:
          memory_stats[process_type][metric] = end_value - start_value
  return memory_stats