code: def test_memory_usage(index_or_series_memory_obj):
      obj = index_or_series_memory_obj
      # Clear index caches so that len(obj) == 0 report 0 memory usage
      if isinstance(obj, Series):
          is_ser = True
          obj.index._engine.clear_mapping()
      else:
          is_ser = False
          obj._engine.clear_mapping()
  
      res = obj.memory_usage()
      res_deep = obj.memory_usage(deep=True)
  
      is_object = is_object_dtype(obj) or (
          is_ser and is_object_dtype(obj.index)
      )
      is_categorical = isinstance(obj.dtype, pd.CategoricalDtype) or (
          is_ser and isinstance(obj.index.dtype, pd.CategoricalDtype)
      )
      is_object_string = is_dtype_equal(obj, "string[python]") or (
          is_ser and is_dtype_equal(obj.index.dtype, "string[python]")
      )
  
      if len(obj) == 0:
          expected = 0
          assert res_deep == res == expected
      elif is_object or is_categorical or is_object_string:
          # only deep will pick them up
          assert res_deep > res
      else:
          assert res == res_deep
  
      # sys.getsizeof will call the .memory_usage with
      # deep=True, and add on some GC overhead
      diff = res_deep - sys.getsizeof(obj)
      assert abs(diff) < 100
  
file_path: pandas/pandas/tests/base/test_misc.py
imports: import sys
  import numpy
  import pytest
  from pandas._config import using_pyarrow_string_dtype
  from pandas.compat import PYPY
  from pandas.core.dtypes.common import is_dtype_equal, is_object_dtype
  import pandas
  from pandas import Index, Series
label_desc: Use image node_id 4 for calling a global function with example usage: test_memory_usage(index_or_series_memory_obj) without return types