code: def assert_no_leak(
      f, num_iters=100000, increase_threshold_absolute_mb=25
  ):
      """Assert memory usage doesn't increase beyond given threshold for f."""
  
      with context.eager_mode():
          # Warm up.
          f()
  
          # Wait for background threads to start up and take over memory.
          # FIXME: The nature of this test leaves few other options. Maybe there
          # is a better way to do this.
          time.sleep(4)
  
          gc.collect()
          initial = memory_profiler.memory_usage(-1)[0]
          instance_count_by_class_before = _instance_count_by_class()
  
          for _ in range(num_iters):
              f()
  
          gc.collect()
          increase = memory_profiler.memory_usage(-1)[0] - initial
  
          assert increase < increase_threshold_absolute_mb, (
              "Increase is too high. Initial memory usage: %f MB. Increase: %f MB. "
              "Maximum allowed increase: %f MB. "
              "Instance count diff before/after: %s"
          ) % (
              initial,
              increase,
              increase_threshold_absolute_mb,
              _instance_count_by_class()
              - instance_count_by_class_before,
          )
  
imports: import collections
  import gc
  import time
  from tensorflow.python.eager import context
label_desc: Use image node_id 2 for calling a global function with example usage: assert_no_leak(f, num_iters, increase_threshold_absolute_mb) without return types