By default, memory_usage() reports the shallow memory footprint of each column — for object-dtype columns, like strings, this shallow count only measures the size of the pointers to the actual string objects, not the strings' own memory, which substantially understates the real total memory usage for text-heavy DataFrames. Passing deep=True computes the true memory usage of those object columns by actually inspecting each individual object's size, which is more accurate but noticeably slower to compute.
1Understanding df.memory_usage()
By default, memory_usage() reports the shallow memory footprint of each column — for object-dtype columns, like strings, this shallow count only measures the size of the pointers to the actual string objects, not the strings' own memory, which substantially understates the real total memory usage for text-heavy DataFrames. Passing deep=True computes the true memory usage of those object columns by actually inspecting each individual object's size, which is more accurate but noticeably slower to compute.
Pass deep=True to memory_usage() specifically when a DataFrame has text/object columns and you need an accurate memory estimate — the default shallow calculation significantly understates memory use for those columns, since it only counts pointer sizes, not the actual string data.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
print(df.memory_usage())2Practical Example
Here is a real-world application of df.memory_usage() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"text": ["short", "a much longer string value"]})
print(df.memory_usage(deep=False))
print(df.memory_usage(deep=True))3Best Practices
Follow these guidelines when working with df.memory_usage():
1. Pass deep=True when a DataFrame has object-dtype columns and you need a genuinely accurate memory estimate, not just a quick approximation
2. Sum memory_usage()'s result to get the DataFrame's total memory footprint in one number
3. Use memory_usage() to identify which specific columns are consuming the most memory before deciding where to optimize dtypes, like downcasting int64 to a smaller integer type
Tip: Pass deep=True to memory_usage() specifically when a DataFrame has text/object columns and you need an accurate memory estimate — the default shallow calculation significantly understates memory use for those columns, since it only counts pointer sizes, not the actual string data.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
print(df.memory_usage())