Memory error resulting from print statements

Perhaps this is a problem others are already aware of, but in case it isn’t, I’m posting it here to make others’ lives easier in future.

We’ve been testing two versions of an EEG task with different stimulus types (text stimuli, and images), and in both experiments the memory usage was rapidly increasing following each trial (to the extent that, at some point, PsychoPy would crash with a memory error). After some problem-solving, it turned out that the issue was that we had included print statements during debugging that would indicate which triggers were being sent to the parallel port, and doing this was for some reason hogging massive amounts of RAM. Turning off the print statements fixed the problem. (Additionally, one of the tasks had warning messages appearing in the output tab which were having the same effect; changing the code to obviate the warnings fixed that.)

So, the moral of the story is: If you’re getting a memory error, it’s possible that minimising whatever is printed as output will help. (The error occurred for us on version 2022 2.2.)

2 Likes

Yeah I bumped into this just now and am having the same problem.
PsychoPy v2023.2.3, standalone on Win11.

I’m taking serial input from an arduino and generally echo the input to the screen to check/debug before parsing to specify visual stimuli, but it’s eating ~0.5MB/sec RAM just printing out the messages. This starts to add up over long experiments and is cumulative over multiple consecutive experiment runs.

As you stated above, this went away when I stopped using print().

Memory usage gradually decreases over 15min or so when no experiment is running, back to more normal levels (even though same information present in Output on Coder and Stdout in Runner).

Anybody have any ideas?

Can you display the message in a text component rather than printing to the runner?

Print statements that occur “each frame” are best avoided. Where I need them for debugging I tend to use something like if frameN % 5 == 0: print(x) to limit the frequency.

1 Like

Thanks for the reply-
Sorry, I’m not sure what you mean by a text component- can you elaborate?

I can probably work around this now that I know what’s causing it (e.g. limiting print() statements as you and the OP suggest), but I’m just confused as to why this occurs.

It’s not that the text itself takes up that much memory, since I’m only printing a few chars every few sec. And the memory goes back down (very) gradually after the experiment is stopped over 15min or so, without anything changing in the Output/Stdout panels, so it’s not that text is being dumped from the history over time…