def heapyprofile():
# pip install guppy
# [works on python 2.7, AFAIK]
from guppy import hpy
import gc
hp = hpy()
ast = parse_file('/tmp/197.c')
gc.collect()
h = hp.heap()
print(h)
python类hpy()的实例源码
def __init__(self,cl, name):
U = PyTango.Util.instance()
import gc,resource
try:
import guppy
heapy = guppy.hpy()
except:guppy,heapy = None,None
DynamicDS.__init__(self,cl,name,_locals={'Util':U,'PyUtil':U,'self':self,'fandango':fandango,
'resource':resource,'gc':gc,'guppy':guppy,'heapy':heapy},
useDynStates=False)
DDebug.init_device(self)
def memory_usage():
logging.info("fuck")
h = hpy()
result = str(h.heap()).replace("\n", "<br>")
return result
def do_debugmem(self, _line):
""" Profile python memory usage. """
from guppy import hpy
heap_stats = hpy()
print(heap_stats.heap())
print()
print(heap_stats.heap().byrcs)
def getObjSize(self, obj, hpy=None):
if hpy:
return float(hpy.iso(obj).domisize) / 1024
else:
return 0
# /Stats entry point
def initialize(self, *args, **kwds):
"""Initialize the scenario tree manager.
A scenario tree manager must be initialized before using it.
"""
init_start_time = time.time()
result = None
try:
if self._options.verbose:
print("Initializing %s with options:"
% (type(self).__name__))
self.display_options()
print("")
############# derived method
result = self._init(*args, **kwds)
#############
if self._options.verbose:
print("%s is successfully initialized"
% (type(self).__name__))
except:
if not self._inside_with_block:
print("Exception encountered. Scenario tree manager "
"attempting to shut down.")
print("Original Exception:")
traceback.print_exception(*sys.exc_info())
self.close()
raise
if self._options.output_times or \
self._options.verbose:
print("Overall initialization time=%.2f seconds"
% (time.time() - init_start_time))
# gather and report memory statistics (for leak
# detection purposes) if specified.
if self._options.profile_memory:
if guppy_available:
print(hpy().heap())
else:
print("Guppy module is unavailable for "
"memory profiling")
self._initialized = True
return result