When I wrote a program myself last year, I was unsure of my memory usage and wanted to find a writing tool to print the memory usage of a program or function.
Here is a record of the basic usage of the last 2 memory detection tools found, as well as the need for future analysis of Python program memory usage.
Memory_profiler module (used with Psutil)
Note: Psutil This module, I love it, it implements a lot of Linux commands the main functions, such as: PS, top, lsof, Netstat, ifconfig, who, DF, kill, free and so on.
Sample code (https://github.com/smilejay/python/blob/master/py2014/mem_profile.py):
Copy Code code as follows:
#!/usr/bin/env python
'''
Created on May 31, 2014
@author: Jay <smile665@gmail.com>
@description: Use Memory_profiler module for profiling programs/functions.
'''
From Memory_profiler Import profile
From Memory_profiler import Memory_usage
Import time
@profile
Def my_func ():
A = [1] * (10 * * 6)
b = [2] * (2 * 10 * * 7)
Del b
Return a
Def cur_python_mem ():
Mem_usage = Memory_usage ( -1, interval=0.2, Timeout=1)
Return Mem_usage
Def f (A, n=100):
Time.sleep (1)
b = [a] * n
Time.sleep (1)
Return b
if __name__ = = ' __main__ ':
A = My_func ()
Print Cur_python_mem ()
Print ""
Print Memory_usage ((f, (1,), {' n ': Int (1e6)}), interval=0.5)
Run the above code and the output is:
Copy Code code as follows:
jay@jay-air:~/workspace/python.git/py2014 $python mem_profile.py
Filename:mem_profile.py
Line # Mem usage Increment line Contents
================================================
8.0 MIB 0.0 MIB @profile
Def my_func ():
15.6 MIB 7.6 MiB a = [1] * (10 * * 6)
168.2 MIB 152.6 MIB b = [2] * (2 * 10 * * 7)
15.6 MiB-152.6 MiB del b
15.6 MIB 0.0 MIB return a
[15.61328125, 15.6171875, 15.6171875, 15.6171875, 15.6171875]
[15.97265625, 16.00390625, 16.00390625, 17.0546875, 23.63671875, 23.63671875, 23.640625]
Guppy (using the Heapy)
Guppy is a umbrella package combining heapy and GSL with support utilities, as the such module that Glue keeps Ther.
Sample code (https://github.com/smilejay/python/blob/master/py2014/try_guppy.py):
Copy Code code as follows:
#!/usr/bin/env python
'''
Created on May 31, 2014
@author: Jay <smile665@gmail.com>
@description: Just try to use Guppy-pe (useing heapy) for memory profiling.
'''
From Guppy import hpy
A = [8] * (10 * * 6)
h = hpy ()
Print H.heap ()
Print H.heap (). more
Print H.heap (). More.more
Note that, to output more information. More usage.
Run the above program, the output result is:
Copy Code code as follows:
jay@jay-air:~/workspace/python.git/py2014 $python try_guppy.py
Partition of a set of 26963 objects. Total size = 11557848 bytes.
Index Count% Size% cumulative% Kind (Class/dict of Class)
0 177 1 8151560 8151560 list
1 12056 996840 9 9148400-Str
2 5999 488232 4 9636632 tuple
3 324 1 283104 2 9919736 dict (no owner)
4 0 216416 2 10136152 dict of module
5 199 1 210856 2 10347008 dict of type
6 1646 6 210688 2 10557696 types. CodeType
7 1610 6 193200 2 10750896 function
8 199 1 177008 2 10927904 type
9 124 0 135328 1 11063232 dict of class
<91 more rows. Type e.g. ' _.more ' to view.>
Index Count% Size% cumulative% Kind (Class/dict of Class)
1045 4 83600 1 11148456 __builtin__.wrapper_descriptor
One 109 0 69688 1 11218144 dict of Guppy.etc.Glue.Interface
389 1 34232 0 11252376 __builtin__.weakref
427 2 30744 0 11283120 types. Builtinfunctiontype
411 2 29592 0 11312712 __builtin__.method_descriptor
0 26200 0 11338912 dict of Guppy.etc.Glue.Share
108 0 25056 0 11363968 __builtin__.set
3 19632 0 11383600 818 int
0 18480 0 11402080 dict of Guppy.etc.Glue.Owner
0 17536 0 11419616 dict of ABC. Abcmeta
<81 more rows. Type e.g. ' _.more ' to view.>
(partial output is omitted later)
In addition, there is also a call "Pysizer" is also done memory profiling, but not how to maintain.