Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding how much memory is being used by an object in Python
Memory management is crucial in Python programming. To measure how much memory an object consumes, Python provides several tools including the built-in sys.getsizeof() function and asizeof() from the pympler package. Understanding memory usage helps optimize your programs and prevent memory-related issues.
Using sys.getsizeof() Function
The sys.getsizeof() function returns the size of an object in bytes. It measures only the direct memory consumption of the object itself, not including referenced objects.
Syntax
sys.getsizeof(object)
The function accepts any Python object and returns its size in bytes.
Example
import sys
# Different data types
number = 42
text = "Tutorials Point"
data_list = [1, 2, 3, 4, 5]
data_dict = {"name": "Python", "version": 3.9}
# Measure memory usage
print("Integer:", sys.getsizeof(number), "bytes")
print("String:", sys.getsizeof(text), "bytes")
print("List:", sys.getsizeof(data_list), "bytes")
print("Dictionary:", sys.getsizeof(data_dict), "bytes")
Integer: 28 bytes String: 64 bytes List: 104 bytes Dictionary: 232 bytes
Complex Objects Example
import sys
# Dictionary with various data types
complex_dict = {
"string": "Hello World",
"integer": 100,
"float": 3.14159,
"list": [10, 20, 30, 40, 50],
"function": lambda x: x ** 2
}
# Function that creates a list
def create_number_list(n):
return [i for i in range(n)]
print("Complex dictionary size:", sys.getsizeof(complex_dict), "bytes")
print("Function size:", sys.getsizeof(create_number_list), "bytes")
print("Generated list size:", sys.getsizeof(create_number_list(1000)), "bytes")
Complex dictionary size: 280 bytes Function size: 144 bytes Generated list size: 8856 bytes
Using asizeof() Function from Pympler
The asizeof() function from the pympler package provides a more comprehensive measurement by including the memory used by all referenced objects.
Syntax
from pympler.asizeof import asizeof asizeof(object)
Note: You need to install pympler first: pip install pympler
Example
from pympler.asizeof import asizeof
# Same objects as before
complex_dict = {
"string": "Hello World",
"integer": 100,
"float": 3.14159,
"list": [10, 20, 30, 40, 50],
"nested": {"a": 1, "b": 2}
}
def create_data_structure(size):
return {"numbers": list(range(size)), "text": "sample" * size}
print("Dictionary with asizeof():", asizeof(complex_dict), "bytes")
print("Function with asizeof():", asizeof(create_data_structure), "bytes")
print("Complex structure:", asizeof(create_data_structure(100)), "bytes")
Comparison of Methods
| Method | Measures | Installation Required | Best For |
|---|---|---|---|
sys.getsizeof() |
Object itself only | No (built-in) | Quick size checks |
pympler.asizeof() |
Object + all references | Yes (pip install pympler) | Detailed memory analysis |
Practical Example - Memory Growth
import sys
# Compare different container sizes
small_list = list(range(10))
medium_list = list(range(100))
large_list = list(range(1000))
print("Small list (10 items):", sys.getsizeof(small_list), "bytes")
print("Medium list (100 items):", sys.getsizeof(medium_list), "bytes")
print("Large list (1000 items):", sys.getsizeof(large_list), "bytes")
# String comparison
short_str = "Hello"
long_str = "Hello" * 100
print("Short string:", sys.getsizeof(short_str), "bytes")
print("Long string:", sys.getsizeof(long_str), "bytes")
Small list (10 items): 184 bytes Medium list (100 items): 856 bytes Large list (1000 items): 8856 bytes Short string: 54 bytes Long string: 549 bytes
Conclusion
Use sys.getsizeof() for quick memory measurements of individual objects. For comprehensive memory analysis including referenced objects, use pympler.asizeof(). Understanding memory usage patterns helps optimize your Python applications and prevent memory bottlenecks.
