To determine the size of an object in Python, use sys.getsizeof() from the sys module. It measures the memory (in bytes) used by the object itself. For example:
import sys
data = [1, 2, 3]
print(sys.getsizeof(data)) # Outputs the size of the list
However, sys.getsizeof() doesn’t include memory used by elements inside collections (e.g., lists or dictionaries). For a complete size, use pympler:
from pympler import asizeof
data = [1, 2, [3, 4]]
print(asizeof.asizeof(data)) # Total memory usage
This helps optimize memory in programs by analyzing object sizes.