You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Creating too many qstr's leads to large memory use #2280
I've narrowed this down a little further. Replacing pickle with json in the above code removes the leak. The following code is the minimum test case I've found for the leak: I suspect the exec statement.
It only leaks if s changes on each iteration - the counter is necessary. In my attempt to find a workround I tried this, which perhaps offers a clue to the cause.
Confirmed. The reason for the increased memory usage is string interning: the string you are evaluating is something like v=('result', '123'), with the 123 changing on each iteration. The '123' is a small string that the parser interns when it parses the code you give to eval/exec/compile. So as iterations go on there the interned strings from previous runs remain and eventually you run out of RAM.
A work-around for the above script is to replace str(count) with count, since then it's not a string but an integer. But that doesn't help the general case.
I wonder if string interning could be limited to just string literals that appear in the programs?
As far as the parser is concerned, the input from a file is equivalent to the input from exec. So there would need to be extra logic to distinguish these.
Activity
peterhinch commentedon Aug 1, 2016
I've narrowed this down a little further. Replacing pickle with json in the above code removes the leak. The following code is the minimum test case I've found for the leak: I suspect the exec statement.
It only leaks if s changes on each iteration - the counter is necessary. In my attempt to find a workround I tried this, which perhaps offers a clue to the cause.
Commenting out the exec statement made no difference. The leak occurs in the compile statement. An attempt at using eval also leaks:
produced
dpgeorge commentedon Aug 4, 2016
Confirmed. The reason for the increased memory usage is string interning: the string you are evaluating is something like
v=('result', '123'), with the 123 changing on each iteration. The'123'is a small string that the parser interns when it parses the code you give to eval/exec/compile. So as iterations go on there the interned strings from previous runs remain and eventually you run out of RAM.A work-around for the above script is to replace
str(count)withcount, since then it's not a string but an integer. But that doesn't help the general case.deshipu commentedon Aug 4, 2016
I wonder if string interning could be limited to just string literals that appear in the programs? Would that make sense?
dpgeorge commentedon Aug 4, 2016
As far as the parser is concerned, the input from a file is equivalent to the input from
exec. So there would need to be extra logic to distinguish these.deshipu commentedon Aug 4, 2016
Ah, you are of course, right, in this case this is actually a string literal being evaluated, I missed that, sorry.
peterhinch commentedon Aug 4, 2016
Thanks for that. I'll figure out if I can apply the workround in my application.
Merge pull request micropython#2280 from xinabox/master
dpgeorge commentedon Jun 23, 2022
Closing due to inactivity. Also #4422 tracks a similar issue.