Skip to content

Instantly share code, notes, and snippets.

@rphlypo
Created February 7, 2014 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rphlypo/5a412dd290845ebd474d to your computer and use it in GitHub Desktop.
Save rphlypo/5a412dd290845ebd474d to your computer and use it in GitHub Desktop.
scipy_tnc memory leak
import scipy.optimize
import numpy as np
from memory_profiler import profile
#@profile
def optim(method="tnc"):
"""
minimal example to test memory leak of the fmin_tnc function
the example exhibits the memory leak in `fmin_tnc` when ran through a
memory profiler. As a comparison, the `fmin_l_bfgs_b` can be run,
showing almost no memory leak.
Typical values are 256MB for `fmin_tnc`, and 0.21MB for `fmin_l_bfgs_b`
input:
-----
method: string, optional
choose either "tnc" or "bfgs"
"""
for z in np.arange(1000):
alpha12 = np.ones((100,))
X = np.random.uniform(low=-1./np.sqrt(3),high=1./np.sqrt(3),
size=(100,100))
Theta11 = X.dot(X.T)/100.
s12 = np.random.normal(size=(100,))
gamma12_ = np.random.normal(size=(100,))
if method == "tnc":
out = scipy.optimize.fmin_tnc(func, gamma12_,
args=(Theta11,s12),
bounds=[(-a,a) for a in alpha12],
disp=0)
elif method == "bfgs":
out = scipy.optimize.fmin_l_bfgs_b(func, gamma12_,
args=(Theta11,s12),
bounds=[(-a,a) for a in alpha12],
disp=0)
return 0
def func(x,A,s):
return 1. / 2 * (x+s).T.dot(A).dot(x+s), A.dot( x+s)
if __name__ == "__main__":
import resource
mem_before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print("Running memory profiling on 'fmin_tnc'")
optim(method="tnc")
mem_after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print "Memory leakage: {0}MB {1}kB".format((mem_after - mem_before)/1024,
(mem_after - mem_before)%1024)
mem_before = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print("Running memory profiling on 'bfgs'")
optim(method="bfgs")
mem_after = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print "Memory leakage: {0}MB {1}kB".format((mem_after - mem_before)/1024,
(mem_after - mem_before)%1024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment