Skip to content
Advertisement

Python: setting memory limit for a particular function call

In a Python script, I want to set a memory limit for a certain function call. I looked at how to limit heap size; however, I don’t want to limit the memory of the entire running Python process — i.e. setting the memory limit before and after the function call.

Is there any way to make a function call with a given amount of memory, so that the memory limit doesn’t affect the caller?

Advertisement

Answer

No, this is impossible. Python doesn’t keep track of which functions are responsible for allocating new memory, and nor does libc, so there’s no way to limit memory usage just by a single function.

In order to do this you would have to modify Python so that you use a new function for allocating memory that requires it to be specified what Python function is responsible, so that it can reject the memory allocation if that function goes over its limit.

The only other way to do this would indeed be to execute the function in a separate process with limited memory, as @JBernardo said.

As for implementing sandboxes, there already are relatively well tested implementations. Is there any reason you can’t use those? In particular see PyPy’s sandboxed VM and the Zope sandbox.

Advertisement