Package: malloc-tracer
malloc_tracerAboutThis is a debugging tool for tracing malloc that occurs inside a
function or class.importnumpyasnpimportmalloc_tracerdeffunc(x,y,z):dataset1=np.empty((100,),dtype=np.float64)print('x',x)dataset1=np.empty((1000,),dtype=np.float64)l=[iforiinrange(100000)]ifx==0:dataset4a=np.empty((100000,),dtype=np.float64)return0elifx==1:dataset4b=np.empty((100000,),dtype=np.float64)return1dataset3=np.empty((3000,),dtype=np.float64)return2tracer=malloc_tracer.Tracer(func)This is equivalent to the following code.importnumpyasnpfromtracemallocimportstart,take_snapshot,stopSNAPSHOT=Nonedeffunc(x,y,z):try:start()dataset1=np.empty((100,),dtype=np.float64)print('x',x)dataset1=np.empty((1000,),dtype=np.float64)l=[iforiinrange(100000)]if(x==0):dataset4a=np.empty((100000,),dtype=np.float64)return0elif(x==1):dataset4b=np.empty((100000,),dtype=np.float64)return1dataset3=np.empty((3000,),dtype=np.float64)return2finally:globalSNAPSHOTSNAPSHOT=take_snapshot()stop()FeatureCompatibilitymalloc_tracer works with Python 3.4 or higher.DependenciesInstallationpip install malloc-tracerUsageTrace a function.importnumpyasnpimportmalloc_tracerdeffunc(x,y,z):dataset1=np.empty((100,),dtype=np.float64)print('x',x)dataset1=np.empty((1000,),dtype=np.float64)l=[iforiinrange(100000)]ifx==0:dataset4a=np.empty((100000,),dtype=np.float64)return0elifx==1:dataset4b=np.empty((100000,),dtype=np.float64)return1dataset3=np.empty((3000,),dtype=np.float64)return2tracer=malloc_tracer.Tracer(func)tracer.trace(target_args=dict(x=1,y=2,z=3))usage1Trace a method.importnumpyasnpimportmalloc_tracerclassKlass(object):CONSTANT='CONSTANT'def__init__(self,value):self._value=valuedefmethod(self,x):dataset1=np.empty((100,),dtype=np.float64)print('x',x)dataset1=np.empty((1000,),dtype=np.float64)l=[iforiinrange(100000)]ifx==0:dataset4a=np.empty((100000,),dtype=np.float64)return0elifx==1:dataset4b=np.empty((100000,),dtype=np.float64)return1dataset3=np.empty((3000,),dtype=np.float64)return2@staticmethoddefsmethod():dataset=np.empty((100,),dtype=np.float64)l=[iforiinrange(100000)]print('Hello')returndataset@classmethoddefcmethod(cls,var):returncls.CONSTANT+varinstance=Klass(1)tracer=malloc_tracer.Tracer(instance.method)tracer.trace(target_args=dict(x=1))usage2aTrace a static method.tracer=malloc_tracer.Tracer(Klass.smethod)tracer.trace(target_args=dict())usage2bTrace a class method.tracer=malloc_tracer.Tracer(Klass.cmethod)tracer.trace(target_args=dict(var='Hello world.'))usage2cDisplays related traces for each file.importnumpyasnpimportmalloc_tracerglobal_var1=Noneglobal_var2=Nonedeffunc2():globalglobal_var1globalglobal_var2global_var1=np.empty((1000,),dtype=np.float64)global_var2=np.empty((10000,),dtype=np.float64)deffunc(x,y,z):dataset1=np.empty((100,),dtype=np.float64)print('x',x)dataset1=np.empty((1000,),dtype=np.float64)l=[iforiinrange(100000)]func2()ifx==0:dataset4a=np.empty((100000,),dtype=np.float64)return0elifx==1:dataset4b=np.empty((100000,),dtype=np.float64)return1dataset3=np.empty((3000,),dtype=np.float64)return2tracer=malloc_tracer.Tracer(func)tracer.trace(target_args=dict(x=1,y=2,z=3),related_traces_output_mode=malloc_tracer.RelatedTracesOutputMode.FOR_EACH_FILE)usage3aDisplays related traces in descending order.tracer=malloc_tracer.Tracer(func)tracer.trace(target_args=dict(x=1,y=2,z=3),related_traces_output_mode=malloc_tracer.RelatedTracesOutputMode.IN_DESCENDING_ORDER)usage3bConvenience function.malloc_tracer.trace(func,target_args=dict(x=1,y=2,z=3),related_traces_output_mode=malloc_tracer.RelatedTracesOutputMode.IN_DESCENDING_ORDER)LicenseThis software is released under the MIT License, see LICENSE.