views:

137

answers:

3

I have just written a .psf file in Python for executing an optimization algorithm for Abaqus package, but after some analysis it stops. Could you please help me and write Python code to free the memory?

Thanks

A: 

by stopping using it when you do not need, python has garbage collector. Set the attributes, and variables to None when you are done with them.

M. Utku ALTINKAYA
using `del` is probably better, that way you're sure you won't use the wrong value later.
tonfa
+1  A: 

There are really only two python options. You can ask the garbage collector to please run. That may or may not do anything useful. Or you can delete a large container.

del my_var_name

If the memory is not really allocated by Python, you will need to use the interfaces of whatever module you are using to free it up.

Christopher
You need to talk about cycles, reference counting and custom `__del__` methods to be more complete. There are some tools to visualize and monitor too the memory too.
tonfa
I wasn't trying to be exhaustively complete, but you comment is useful.
Christopher
+2  A: 

You don't really explicitly free memory in Python. What you do is stop referencing it, and it gets freed automatically. Although del does this, it's very rare that you really need to use it in a well designed application.

So this is really a question of how not to use so much memory in Python. I'd say the main hint there is to try to refactor your program to use generators, so that you don't have to hold all the data in memory at once.

Lennart Regebro
Even with generators it should be possible to create reference cycles that won't be collected easily.
tonfa
Python will detect most cyclic references and garbage collect them too. But sure, it's possible.
Lennart Regebro