Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:08:28+00:00 2026-05-28T01:08:28+00:00

This bug report states that the Python interpreter, as of June 2007, will not

  • 0

This bug report states that the Python interpreter, as of June 2007, will not clean up all allocated memory after calling Py_Finalize in a C/C++ application with an embedded Python interpreter. It was recommended to call Py_Finalize once at application termination.

This bug report states that as of version 3.3 and March 2011 the interpreter still leaks memory.

Does anyone know the current state of this issue? I am concerned because I have an application in which the interpreter is called multiple times per running instance and I am experiencing memory leaks.

I am already using boost::python to handle reference counts and I clear the global dictionary of all references created by running a Python program in between runs. I have some singleton classes – might these be the problem?

Is this a tractable issue or is it a bug in the Python interpreter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:08:29+00:00Added an answer on May 28, 2026 at 1:08 am

    You can see that the bug (the first one, from 2007) is closed as “wontfix” by nnorwitz, and his post is in the bug report.

    Why do you call Py_Initialize/Py_Finalize more than once?
    Why not do something like this (I’m kinda mixing C and
    Python for convenience):

    /* startup */
    Py_Initialize();
    
    /* do whatever */
    while (moreFiles()) {
        PyRun_SimpleString("execfile('%s')" % nextFile());
        /* do whatever */
    }
    
    /* shutdown */
    Py_Finalize();
    

    The problem is that most people who write Python modules don’t worry about what happens if their module gets finalized and reinitialized, and often don’t care about cleaning up during finalization. Module authors know that all memory is released when the process exits, and don’t bother with anything more than that.

    So it’s not really one bug, it’s really a thousand bugs — one for each extension module. It’s an enormous amount of work for a bug that affects a minority of users, most of whom have a viable workaround.

    You can always just omit the call to Py_Finalize, calling Py_Initialize a second time is a no-op. This means your application will use additional memory usage when you first run a Python script, and that additional memory won’t get returned to the OS until you exit. As long as you’re still running Python scripts every once in a while, I wouldn’t categorize it as a leak. Your application might not be Valgrind-clean, but it’s better than leaking like a sieve.

    If you need to unload your (pure) Python modules to avoid leaking memory, you can do that. Just delete them from sys.modules.

    Drawbacks of Py_Finalize: If you are executing Python scripts repeatedly, it doesn’t make much sense to run Py_Finalize between them. You’ll have to reload all the modules every time you reinitialize; my Python loads 28 modules at boot.

    Additional commentary: The bug is not limited to Python. A significant amount of the library code in any language will leak memory if you try to unload and reload libraries. Lots of libraries call into C code, lots of C programmers assume that their libraries gets loaded once and unloaded when the process exits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I had seen a bug report about this on the jQuery site,
We have this bug that only appears 30% of the time for the Release
I am trying to detetct/work around this bug in RSS elements. That means I
I have recently discovered that I am affected by this bug http://www.mail-archive.com/mono-bugs@lists.ximian.com/msg71515.html Well, at
I had this nasty bug that disappeared in the past but now after quite
You may consider this a bug report, however I'm curious if I am terribly
In this MSO bug report , our very own waffles makes the following observation:
in the last line in the following bug report https://github.com/grails/grails-core/issues/5296 it is stated that;
UPDATE 2/13/2012: Accepted an answer, explained that this behavior is a bug, and noted
I just stumbled across this bug in some legacy code: class MyAPIHandler { private:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.