First, I need to install the psutil python module for the example of this tutorial.
C:\Python373>cd Scripts
C:\Python373\Scripts>pip install psutil
Python memory monitor is very important for debugging application performance and fix bugs.You can solve this issue is the python module named memory_profiler, see more here.
Let's install this python module with the pip python tool:
C:\Python373\Scripts>pip install memory_profiler
Let's start with a simple python script example:import psutil
def test_psutil():
# gives a single float value
print(psutil.cpu_percent())
# gives an object with many fields
print(psutil.virtual_memory())
# you can convert that object to a dictionary
print(dict(psutil.virtual_memory()._asdict()))
if __name__ == '__main__':
test_psutil()
C:\Python373>python psutil_001.py
0.0
svmem(total=4171108352, available=1153679360, percent=72.3, used=3017428992, fre
e=1153679360)
{'total': 4171108352, 'available': 1153671168, 'percent': 72.3, 'used': 30174371
84, 'free': 1153671168}
The same result can see if you use memory_profilerC:\Python373>python -m memory_profiler psutil_001.py
100.0
svmem(total=4171108352, available=1149018112, percent=72.5, used=3022090240, fre
e=1149018112)
{'total': 4171108352, 'available': 1149087744, 'percent': 72.5, 'used': 30220206
08, 'free': 1149087744}
Let's decorate python source code with @profile annotation to have a good output.import psutil
@profile
def test_psutil():
# gives a single float value
print(psutil.cpu_percent())
# gives an object with many fields
print(psutil.virtual_memory())
# you can convert that object to a dictionary
print(dict(psutil.virtual_memory()._asdict()))
if __name__ == '__main__':
test_psutil()
Sure, this error tells us the decorate not working in the default way.
C:\Python373>python psutil_001.py
Traceback (most recent call last):
File "psutil_001.py", line 2, in
@profile
NameError: name 'profile' is not defined
In this case, the decorate profile works great with the python module and give us all the information we need:C:\Python373>python -m memory_profiler psutil_001.py
100.0
svmem(total=4171108352, available=1022672896, percent=75.5, used=3148435456, fre
e=1022672896)
{'total': 4171108352, 'available': 1022783488, 'percent': 75.5, 'used': 31483248
64, 'free': 1022783488}
Filename: psutil_001.py
Line # Mem usage Increment Line Contents
================================================
2 15.219 MiB 15.219 MiB @profile
3 def test_psutil():
4 # gives a single float value
5 15.230 MiB 0.012 MiB print(psutil.cpu_percent())
6 # gives an object with many fields
7 15.230 MiB 0.000 MiB print(psutil.virtual_memory())
8 # you can convert that object to a dicti
onary
9 15.234 MiB 0.004 MiB print(dict(psutil.virtual_memory()._asdi
ct()))
Let's test with another python script named 001.py :from memory_profiler import profile
@profile(precision=4)
def test():
a = 0
a = a + 1
if __name__ == "__main__":
test()
The result with precision=4 is this:C:\Python373>python -m memory_profiler 001.py
Filename: 001.py
Line # Mem usage Increment Line Contents
================================================
3 15.3945 MiB 15.3945 MiB @profile(precision=4)
4 def test():
5 15.3945 MiB 0.0000 MiB a = 0
6 15.3945 MiB 0.0000 MiB a = a + 1
If we change the precision=1 then this is the result:C:\Python373>python -m memory_profiler 002.py
Filename: 002.py
Line # Mem usage Increment Line Contents
================================================
3 15.4 MiB 15.4 MiB @profile(precision=1)
4 def test():
5 15.4 MiB 0.0 MiB a = 0
6 15.4 MiB 0.0 MiB a = a + 1
A good source of information for this python module can be found at GitHub.