This project is archived and is in readonly mode.

#160 ✓ hold
Psycopg website

Memory leak in ebedded python

Reported by Psycopg website | May 6th, 2013 @ 02:02 AM

Submitted by: alexsobral@gmail.com

I'm developing a program with embedded Pythhon 3.3.
When I'm using the psysopg2 library I'm having a big memory link only by importing the library.
The problem can be reproduced with the python embed example '_testembed.c' with minor changes and importing psycopg2.

#include <Python.h>
#include <stdio.h>

void print_subinterp(void)
{
    /* Just output some debug stuff */
    PyThreadState *ts = PyThreadState_Get();
    //printf("interp %p, thread state %p: ", ts->interp, ts);
    //fflush(stdout);
    PyRun_SimpleString(
        "import psycopg2;"
        //"print('id(modules) =', id(sys.modules));"
        //"sys.stdout.flush()"
    );
}

int main(int argc, char *argv[])
{
    PyThreadState *mainstate, *substate;
#ifdef WITH_THREAD
    PyGILState_STATE gilstate;
#endif
    int i, j;

    for (i=0; i<3; i++) {
        printf("--- Pass %d ---\n", i);
        /* HACK: the "./" at front avoids a search along the PATH in
           Modules/getpath.c */
        Py_SetProgramName(L"./_testembed");
        Py_Initialize();
        mainstate = PyThreadState_Get();

#ifdef WITH_THREAD
        PyEval_InitThreads();
        PyEval_ReleaseThread(mainstate);

        gilstate = PyGILState_Ensure();
#endif
        print_subinterp();
        PyThreadState_Swap(NULL);

        for (j=0; j<3000; j++) {
            substate = Py_NewInterpreter();
            print_subinterp();
            Py_EndInterpreter(substate);
        }

        PyThreadState_Swap(mainstate);
        print_subinterp();
#ifdef WITH_THREAD
        PyGILState_Release(gilstate);
#endif

        PyEval_RestoreThread(mainstate);
        Py_Finalize();
    }
    return 0;
}

Thanks in advance.

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo May 6th, 2013 @ 09:21 AM

    • State changed from “new” to “hold”

    Psycopg doesn't support unloading: it has never been tested for this usage. I'm not closing the issue as WONTFIX straight ahead but I won't be working on it. If somebody has reasonable patches to submit that's ok.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

WARNING: the informations in this tracker are archived. Please submit new tickets or comments to the new tracker.


Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins

Pages