I have a question regarding python memory management. I have the following code
def operation(data):
#some manipulations on data
result=something.do(data)
#some manipulations on result
return result
Now I am calling this function operation many times (probably like more than 200 times). Does python use a same memory for the result variable everytime I call operation?
As in C we can use Malloc to allocate memory once and use to the same memory inorder to avoid fragmentation.
4 Answers 4
The whole point of high-level languages like Python is that they free you from having to worry about memory management. If exact control over memory allocation is important to you, you should write C. If not, you can write Python.
As most Python programmers will tell you from their experience, manual memory management isn't nearly as important as you think it is.
Comments
No it is not but it is not a big deal because once you return from the function, the variable is deleted so there is no memoru-capacity issues involved. If you are talking performance level then it will not matter that much in terms of performance.
2 Comments
No, it does not.
You can, however, write optimized code in C and use it in python:
http://docs.python.org/2/extending/extending.html
This will help if you are concerned about performance.
Comments
Your question is very well valid and as you just anticipated above code might create small fragments of free memory chunks. But interesting point to be noted here is: this free chunks won't be returned back to the Operating system, rather python's memory manager manage its own chunks of free memory blocks.
But again, this free memory blocks can be used by python to allocate same block to next request.
Beautiful explanation of the same given at: http://deeplearning.net/software/theano/tutorial/python-memory-management.html