(f, config, namespaces=None, args=(), kwargs=None)
return metrics
This will track all memory (de-)allocations that occur during the lifetime of function ``f``.
    The only assumption is that the ``config`` dictionary has an entry ``profile_memory`` that
    points to a directory with which to output the information for later consumption.

    The available options (without prefix) are:

      - frames: the number of stack frames to consider
      - gc: whether or not to run the garbage collector before each snapshot to remove noise
      - combine: whether or not to aggregate over all traceback frames. useful only to tell
                 which particular usage of a function triggered areas of interest
      - sort: what to group results by between: lineno | filename | traceback
      - limit: the maximum number of sorted results to show
      - diff: how to order diff results between:
                * absolute: absolute value of the difference between consecutive snapshots
                * positive: same as absolute, but memory increases will be shown first
      - filters: comma-separated list of file path glob patterns to filter by
      - unit: the binary unit to represent memory usage (kib, mb, etc.). the default is dynamic
      - verbose: whether or not to include potentially noisy sources

    :param f: the function to trace
    :param config: a dictionary of options prefixed by ``profile_memory_``
    :param namespaces: if specified, additional sub-directories under ``profile_memory`` root directory
    :param args: arguments to pass to function ``f``
    :param kwargs: keyword arguments to pass to function ``f``
    :return:
This will track all memory (de-)allocations that occur during the lifetime of function ``f``.
    The only assumption is that the ``config`` dictionary has an entry ``profile_memory`` that
    points to a directory with which to output the information for later consumption.
This will track all memory ( de - ) allocations that occur during the lifetime of function f . The only assumption is that the config dictionary has an entry profile_memory that points to a directory with which to output the information for later consumption .
def profile_memory(f, config, namespaces=None, args=(), kwargs=None):
    """
    This will track all memory (de-)allocations that occur during the lifetime of function ``f``.
    The only assumption is that the ``config`` dictionary has an entry ``profile_memory`` that
    points to a directory with which to output the information for later consumption.

    The available options (without prefix) are:

      - frames: the number of stack frames to consider
      - gc: whether or not to run the garbage collector before each snapshot to remove noise
      - combine: whether or not to aggregate over all traceback frames. useful only to tell
                 which particular usage of a function triggered areas of interest
      - sort: what to group results by between: lineno | filename | traceback
      - limit: the maximum number of sorted results to show
      - diff: how to order diff results between:
                * absolute: absolute value of the difference between consecutive snapshots
                * positive: same as absolute, but memory increases will be shown first
      - filters: comma-separated list of file path glob patterns to filter by
      - unit: the binary unit to represent memory usage (kib, mb, etc.). the default is dynamic
      - verbose: whether or not to include potentially noisy sources

    :param f: the function to trace
    :param config: a dictionary of options prefixed by ``profile_memory_``
    :param namespaces: if specified, additional sub-directories under ``profile_memory`` root directory
    :param args: arguments to pass to function ``f``
    :param kwargs: keyword arguments to pass to function ``f``
    :return:
    """
    if kwargs is None:
        kwargs = {}

    frames = int(config.get('profile_memory_frames', DEFAULT_FRAMES))
    run_gc = bool(int(config.get('profile_memory_gc', DEFAULT_GC)))

    try:
        gc.disable()
        gc.collect()

        tracemalloc.start(frames)

        f(*args, **kwargs)

        if run_gc:
            gc.collect()

        snapshot = tracemalloc.take_snapshot()
    finally:
        tracemalloc.stop()
        gc.enable()

    verbose = bool(int(config.get('profile_memory_verbose', DEFAULT_VERBOSITY)))
    if not verbose:
        snapshot = snapshot.filter_traces(
            (
                tracemalloc.Filter(False, '<frozen importlib._bootstrap>'),
                tracemalloc.Filter(False, '<frozen importlib._bootstrap_external>'),
                tracemalloc.Filter(False, '<unknown>'),
                tracemalloc.Filter(False, tracemalloc.__file__),
                tracemalloc.Filter(False, __file__),
            )
        )

    filters = config.get('profile_memory_filters')
    if filters:
        snapshot = snapshot.filter_traces([tracemalloc.Filter(True, pattern) for pattern in filters.split(',')])

    combine = bool(int(config.get('profile_memory_combine', DEFAULT_COMBINE)))
    sort_by = config.get('profile_memory_sort', DEFAULT_SORT_KEY)
    limit = int(config.get('profile_memory_limit', DEFAULT_KEY_LIMIT))
    diff_order = config.get('profile_memory_diff', DEFAULT_DIFF_ORDER)
    unit = config.get('profile_memory_unit', DEFAULT_UNIT)
    unit_formatter = get_unit_formatter(unit)

    # Metrics to send
    metrics = []

    # We're running on a live Agent
    if 'profile_memory' not in config:
        gather_top(metrics, None, snapshot, unit_formatter, sort_by, limit, combine)
        return metrics

    if namespaces:
        # Colons can't be part of Windows file paths
        namespaces = [n.replace(':', '_') for n in namespaces]
        location = os.path.join(config['profile_memory'], *namespaces)
    else:
        location = config['profile_memory']

    if not os.path.isdir(location):
        os.makedirs(location)

    # First, write the prettified snapshot
    snapshot_dir = os.path.join(location, 'snapshots')
    if not os.path.isdir(snapshot_dir):
        os.makedirs(snapshot_dir)

    new_snapshot = os.path.join(snapshot_dir, get_timestamp_filename('snapshot'))
    gather_top(metrics, new_snapshot, snapshot, unit_formatter, sort_by, limit, combine)

    # Then, compute the diff if there was a previous run
    previous_snapshot_dump = os.path.join(location, 'last-snapshot')
    if os.path.isfile(previous_snapshot_dump):
        diff_dir = os.path.join(location, 'diffs')
        if not os.path.isdir(diff_dir):
            os.makedirs(diff_dir)

        previous_snapshot = tracemalloc.Snapshot.load(previous_snapshot_dump)

        # and write it
        new_diff = os.path.join(diff_dir, get_timestamp_filename('diff'))
        gather_diff(metrics, new_diff, snapshot, previous_snapshot, unit_formatter, sort_by, limit, combine, diff_order)

    # Finally, dump the current snapshot for doing a diff on the next run
    snapshot.dump(previous_snapshot_dump)

    return metrics
def profile_memory ( f , config , namespaces = None , args = ( ) , kwargs = None ) : if kwargs is None : kwargs = { } frames = int ( config . get ( 'profile_memory_frames' , DEFAULT_FRAMES ) ) run_gc = bool ( int ( config . get ( 'profile_memory_gc' , DEFAULT_GC ) ) ) try : gc . disable ( ) gc . collect ( ) tracemalloc . start ( frames ) f ( * args , * * kwargs ) if run_gc : gc . collect ( ) snapshot = tracemalloc . take_snapshot ( ) finally : tracemalloc . stop ( ) gc . enable ( ) verbose = bool ( int ( config . get ( 'profile_memory_verbose' , DEFAULT_VERBOSITY ) ) ) if not verbose : snapshot = snapshot . filter_traces ( ( tracemalloc . Filter ( False , '<frozen importlib._bootstrap>' ) , tracemalloc . Filter ( False , '<frozen importlib._bootstrap_external>' ) , tracemalloc . Filter ( False , '<unknown>' ) , tracemalloc . Filter ( False , tracemalloc . __file__ ) , tracemalloc . Filter ( False , __file__ ) , ) ) filters = config . get ( 'profile_memory_filters' ) if filters : snapshot = snapshot . filter_traces ( [ tracemalloc . Filter ( True , pattern ) for pattern in filters . split ( ',' ) ] ) combine = bool ( int ( config . get ( 'profile_memory_combine' , DEFAULT_COMBINE ) ) ) sort_by = config . get ( 'profile_memory_sort' , DEFAULT_SORT_KEY ) limit = int ( config . get ( 'profile_memory_limit' , DEFAULT_KEY_LIMIT ) ) diff_order = config . get ( 'profile_memory_diff' , DEFAULT_DIFF_ORDER ) unit = config . get ( 'profile_memory_unit' , DEFAULT_UNIT ) unit_formatter = get_unit_formatter ( unit ) # Metrics to send metrics = [ ] # We're running on a live Agent if 'profile_memory' not in config : gather_top ( metrics , None , snapshot , unit_formatter , sort_by , limit , combine ) return metrics if namespaces : # Colons can't be part of Windows file paths namespaces = [ n . replace ( ':' , '_' ) for n in namespaces ] location = os . path . join ( config [ 'profile_memory' ] , * namespaces ) else : location = config [ 'profile_memory' ] if not os . path . isdir ( location ) : os . makedirs ( location ) # First, write the prettified snapshot snapshot_dir = os . path . join ( location , 'snapshots' ) if not os . path . isdir ( snapshot_dir ) : os . makedirs ( snapshot_dir ) new_snapshot = os . path . join ( snapshot_dir , get_timestamp_filename ( 'snapshot' ) ) gather_top ( metrics , new_snapshot , snapshot , unit_formatter , sort_by , limit , combine ) # Then, compute the diff if there was a previous run previous_snapshot_dump = os . path . join ( location , 'last-snapshot' ) if os . path . isfile ( previous_snapshot_dump ) : diff_dir = os . path . join ( location , 'diffs' ) if not os . path . isdir ( diff_dir ) : os . makedirs ( diff_dir ) previous_snapshot = tracemalloc . Snapshot . load ( previous_snapshot_dump ) # and write it new_diff = os . path . join ( diff_dir , get_timestamp_filename ( 'diff' ) ) gather_diff ( metrics , new_diff , snapshot , previous_snapshot , unit_formatter , sort_by , limit , combine , diff_order ) # Finally, dump the current snapshot for doing a diff on the next run snapshot . dump ( previous_snapshot_dump ) return metrics