1

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.

asked Dec 28, 2013 at 20:38

4 Answers 4

3

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.

answered Dec 28, 2013 at 20:56
Sign up to request clarification or add additional context in comments.

Comments

3

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.

answered Dec 28, 2013 at 20:47

2 Comments

But will it not lead to fragmentation of memory if I am keeping my program in execution for a long time?
Good question. You dont really have control over this. This will be the job of the OS. Whatever variables you are using are actually in the virtual memory. For instance in C, your function local variables are stored in the virtual memory stack, along with the function arguments and the return address, etc... What happens in the "physical" memory is up to the OS. Once your function returns, all the function contents stored in the stack will not exist anymore.
2

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.

answered Dec 28, 2013 at 20:41

Comments

0

@heisenberg

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

answered Apr 11, 2016 at 4:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.