Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Memory leak in ebedded python

#160

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.

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

State: hold
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo
    • 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.

    May 6th, 2013 @ 09:21 AM