1

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

3
  • can you bring a short example which accumulates memory so we can tell what your problem is? Commented Aug 25, 2009 at 21:20
  • which version of python? Commented Aug 25, 2009 at 21:53
  • Duplicate: stackoverflow.com/questions/1316767/… Commented Aug 25, 2009 at 21:55

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

2 Comments

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.
I wasn't trying to be exhaustively complete, but you comment is useful.
0

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.

1 Comment

using del is probably better, that way you're sure you won't use the wrong value later.

Your Answer

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