pythontutor-dev / pythontutor

10 stars 4 forks source link

possible memory leak when importing Python modules #93

Open pgbovine opened 5 years ago

pgbovine commented 5 years ago

From a user on 2017-12-17: {

Dear Philip,

There seems to be some memory leak with the Python Tutor when an import statement is executed repeatedly. Here's an example.

The memory is exceeded because the regular expressions module is imported in the body of the splitCode function, which is executed a couple of times. Python should only import the module once, but I'm afraid it's created each time the function is called. Moving the import outside of the function removes the memory issue, so that's why I'm think it must leak.

We run the OPT on our own servers, and there we see the same issue.

}

pdawyndt commented 5 years ago

We have integrated the Python Tutor in Dodona, an intelligent tutoring system that acts as an online co-teacher when students learn computer programming. I can confirm that in this context, this issue (memory leaks caused by repeated imports) is the number one reason why the Python Tutor crashes. So resolving this issue would be a great help.

pgbovine commented 5 years ago

thanks for your bug report! can you send me some additional snippets of code that exhibits this memory leak issue so that i can debug better?

pdawyndt commented 5 years ago

Tried to find a minimal example where it occurs:

def importmodule():
    import string

for _ in range(100):
    importmodule()

Memory leak seems to cause this code to crash. Issue does not occur if math module is imported instead of the string module, nor if loop is only executed 10 times, nor if import happens outside function call as in

for _ in range(100):
    import string