41

I have Python extensions implemented on C++ classes. I don't have a C++ target to run valgrind with. I want to use valgrind for memory check.

Can I use valgrind with Python?

3 Answers 3

53

Yes, you can use valgrind with Python. You just need to use the valgrind suppression file provided by the Python developers, so you don't get a bunch of false positives due to Python's custom memory allocation/reallocation functions.

The valgrind suppression file can be found here: http://svn.python.org/projects/python/trunk/Misc/valgrind-python.supp

IMPORTANT: You need to uncomment the lines for PyObject_Free and PyObject_Realloc in the suppression file*.

The recommended usage syntax is:

$ valgrind --tool=memcheck --suppressions=valgrind-python.supp \
                                          python -E -tt ./my_python_script.py

See also this README file from the Python SVN repo which describes the different ways of using Python with valgrind: http://svn.python.org/projects/python/trunk/Misc/README.valgrind

* - Alternatively, you can recompile Python with PyMalloc disabled, which allows you to catch more memory leaks that won't show up if you just suppress PyMalloc.

4
  • 3
    Python 2.7 seems pretty naughty. An empty script (valgrind --leak-check=yes --suppressions=valgrind-python.supp python -tt -E "") gives "315 errors from 315 contexts (suppressed: 2 from 2)" on Fedora 16.
    – Ling
    Mar 12, 2012 at 21:44
  • 2
    @Ling Did you remember to uncomment the lines in Misc/valgrind-python.supp that suppress the warnings for PyObject_Free and PyObject_Realloc? Mar 12, 2012 at 21:46
  • 1
    I did. That's why I was surprised.
    – Ling
    Mar 13, 2012 at 1:45
  • 3
    In my quick test just opening the python interpreter, I went from: ==9872== ERROR SUMMARY: 793 errors from 75 contexts (suppressed: 15 from 10) to ==9920== ERROR SUMMARY: 31 errors from 23 contexts (suppressed: 777 from 62) with the suppression file (PyObject_Free and PyObject_Realloc uncommented). I did try the python configure flags, but they didn't seem to get rid of the remaining errors.
    – amos
    Jul 29, 2014 at 21:34
9

In Python 2.7 and 3.2 there is now a --with-valgrind compile-time flag that allows the Python interpreter to detect when it runs under valgrind and disables PyMalloc. This should allow you to more accurately monitor your memory allocations than otherwise, as PyMalloc just allocates memory in big chunks.

2
  • is this when you run python or when you are compiling python? I am running 2.7.1 and could not just do 'valgrind python --with-valgrind myscript.py'
    – oob
    Nov 27, 2011 at 19:16
  • 2
    Sorry, I should have been more clear. It's a compile-time flag which will configure Python to detect when it's running under valgrind. Some details are here: bugs.python.org/issue2422 Nov 27, 2011 at 20:19
0

Yes you can: you do have a target to run valgrind with -- it's the python interpreter itself:

valgrind python foo.py

However, the results of above may not be very satisfactory -- Python is built in opt mode and with a special malloc, which may drown you in false positives.

You'll likely get better results by first building a debug version of Python. Start here.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.