Numpy Memory Error and GLException: b'out of memory'

OS (e.g. Win10): Win11
PsychoPy version:2025.1.1 Py 3.10
Standard Standalone Installation? (y/n) yes

In my experiment, I will need to provide immediate feedback based on participants’ responses. Basically, in each trial, the screen shows a reward whenever participants click one of the targets on the display. There are multiple targets on one display. The amount of reward start from 0 in each trial. The simplified code is the following:

------------before experiment--------
def makeApples(appleCounts,applesList,appleSizes,applePosX):
    apples=[]
    for i in range(appleCounts):
        apple = visual.ImageStim(
        win=win, texRes=128.0,interpolate = True
        )
        apples.append(apple)
    return apples
    
def drawApples(apples):
    for each in apples:
        each.draw()

def makeBoxes(boxCounts,boxPosX):
    boxes=[]
    for i in range(boxCounts):
        box = visual.ImageStim(
        win=win, image="image/box.png",interpolate = True     
        )
        boxes.append(box)
    return boxes
    
def drawBoxes(boxes):
    for each in boxes:
        each.draw()


--------------each Frame-------------

for clickable in treeImages:
     if imageMouse2.isPressedIn(clickable): 
        if clickedImage == None:
            clickedImage = clickable
            tempString = clickedImage.name
            treeNum = int(tempString.replace("tree", ""))
            applesList.append(appleImages[treeNum].image)
            appleSizes.append(appleSize[treeNum][1][applesCount])
            appleSizesLists.append(appleSize[treeNum][0])
            applesCount +=1
            break
        else:
            break
    

boxes=makeBoxes(horizon,rewardPosX)
drawBoxes(boxes)
apples=makeApples(applesCount,applesList,appleSizes,rewardPosX)
drawApples(apples)

Then I went into the following problem after around 20 trials:

######## Experiment ended with exit code 1 [pid:30984] #########
Generating PsychoPy script…

2668.4706 INFO Loaded monitor calibration from [‘2024_10_22 15:53’]
2668.5433 INFO Loaded monitor calibration from [‘2024_10_22 15:53’]
Traceback (most recent call last):
File “D:\documents\research_projects\Leechi\a2_material_and_methods\test\horizonsTask_trees\horizonTreeTask_lastrun.py”, line 1776, in
run(
File “D:\documents\research_projects\Leechi\a2_material_and_methods\test\horizonsTask_trees\horizonTreeTask_lastrun.py”, line 1183, in run
boxes=makeBoxes(horizon,rewardPosX)
File “D:\documents\research_projects\Leechi\a2_material_and_methods\test\horizonsTask_trees\horizonTreeTask_lastrun.py”, line 403, in makeBoxes
box = visual.ImageStim(
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\image.py”, line 111, in init
self.setImage(image, log=False)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\image.py”, line 452, in setImage
setAttribute(self, ‘image’, value, log)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py”, line 191, in setAttribute
setattr(self, attrib, value)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py”, line 69, in set
newValue = self.func(obj, value)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\image.py”, line 427, in image
self.isLumImage = self._createTexture(
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\basevisual.py”, line 1149, in _createTexture
intensity = numpy.array(im)
numpy._core._exceptions._ArrayMemoryError: Unable to allocate 3.44 MiB for an array with shape (950, 950, 4) and data type uint8
215.8139 WARNING Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
215.8564 WARNING Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
215.8565 WARNING Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
215.8565 WARNING Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
######### Experiment ended with exit code 1 [pid:7412] #########

Here, the message shows the error is from the boxes, but sometimes it can also come from the apples.

I tried the following code as suggested by AI to free up the memory, but it doesn’t work:

treeImages.clear()
apples.clear()
boxes.clear()
del treeImages
del apples
del boxes
gc.collect()

Then I try to reduce the pixel of all the pictures and put the texRes=64.0. It can go on for almost 100 trials. But it either went into the same numpy Memory Error or another memory problem, which comes with the whole screen going dark and coming back on again:

Generating PsychoPy script...

## Running: 
75.0612     INFO     Loaded monitor calibration from ['2024_10_22 15:53']
75.1332     INFO     Loaded monitor calibration from ['2024_10_22 15:53']
50.7363     WARNING     Keypress was given unknown key code (176)
Traceback (most recent call last):
50.9105     WARNING     Keypress was given unknown key code (176)
51.9067     WARNING     Keypress was given unknown key code (176)
52.0430     WARNING     Keypress was given unknown key code (176)
52.1744     WARNING     Keypress was given unknown key code (176)
52.3450     WARNING     Keypress was given unknown key code (176)
52.5084     WARNING     Keypress was given unknown key code (176)
52.6742     WARNING     Keypress was given unknown key code (176)
52.8415     WARNING     Keypress was given unknown key code (176)
53.0088     WARNING     Keypress was given unknown key code (176)
53.1391     WARNING     Keypress was given unknown key code (176)
  File "...", line 1773, in <module>
    run(
  File "...", line 1297, in run
    win.flip()
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\window.py", line 1419, in flip
    GL.glFinish()
  File "C:\Program Files\PsychoPy\lib\site-packages\pyglet\gl\lib.py", line 106, in errcheck
    raise GLException(msg)
pyglet.gl.lib.GLException: b'out of memory'
50.7363     WARNING     Keypress was given unknown key code (176)
50.9105     WARNING     Keypress was given unknown key code (176)
51.9067     WARNING     Keypress was given unknown key code (176)
52.0430     WARNING     Keypress was given unknown key code (176)
52.1744     WARNING     Keypress was given unknown key code (176)
52.3450     WARNING     Keypress was given unknown key code (176)
52.5084     WARNING     Keypress was given unknown key code (176)
52.6742     WARNING     Keypress was given unknown key code (176)
52.8415     WARNING     Keypress was given unknown key code (176)
53.0088     WARNING     Keypress was given unknown key code (176)
53.1391     WARNING     Keypress was given unknown key code (176)
1273.9413     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
1274.0330     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
1274.0331     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
1274.0331     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
Exception ignored in atexit callback: <function flush at 0x0000022726AC08B0>
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\logging.py", line 323, in flush
    logger.flush()
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\logging.py", line 308, in flush
    formatted[thisEntry] = self.format.format(**thisEntry.__dict__)
MemoryError: 
50.7363     WARNING     Keypress was given unknown key code (176)
50.9105     WARNING     Keypress was given unknown key code (176)
51.9067     WARNING     Keypress was given unknown key code (176)
52.0430     WARNING     Keypress was given unknown key code (176)
52.1744     WARNING     Keypress was given unknown key code (176)
52.3450     WARNING     Keypress was given unknown key code (176)
52.5084     WARNING     Keypress was given unknown key code (176)
52.6742     WARNING     Keypress was given unknown key code (176)
52.8415     WARNING     Keypress was given unknown key code (176)
53.0088     WARNING     Keypress was given unknown key code (176)
53.1391     WARNING     Keypress was given unknown key code (176)
1273.9413     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
1274.0330     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
1274.0331     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
1274.0331     WARNING     Stopping key buffers but this could be dangerous ifother keyboards rely on the same.
Exception ignored in: <function _Logger.__del__ at 0x0000022726AC0C10>
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\logging.py", line 256, in __del__
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\logging.py", line 308, in flush
MemoryError: 
######## Experiment ended with exit code 1 [pid:22432] #########

I need to do at least 200 trials for the experiment, and I wonder if anyone has any idea how to solve it. Thank you so much in advance.

Hello

AI is usually not so good in programming PsychoPy-experiments. Here you create the objects apple and boxes in each frame.

boxes = makeBoxes(horizon, rewardPosX)  # ← New objects
drawBoxes(boxes)
apples = makeApples(applesCount, ...)    # ← New objects
drawApples(apples)

That means you create frames x (horizon + applesCount) ImageStim-objects in memory! It is a better approach ot create the objects once before each Frame and then only assign new values in each frame.

For example,

for i in range(min(horizon, len(boxes))):
    boxes[i].pos = (rewardPosX[i], 0) 

Best wishes Jens

2 Likes

Thank you so much. This is very helpful.

In the meantime, I am also able to run the experiment smoothly with an older version of PsychoPy (2022.1.4).

I try to use the following function to check the memory use:

import psutil

# Log memory usage after each trial
def log_memory():
    process = psutil.Process()
    mem_info = process.memory_info()
    print(f"Memory usage: {mem_info.rss / (1024 * 1024):.2f} MB")

The log information in the PsychoPy2022.1.4 shows the memory is quite stable from trial to trial:

Memory usage: 747.00 MB
Memory usage: 684.78 MB
Memory usage: 710.51 MB
Memory usage: 581.32 MB
Memory usage: 664.84 MB
Memory usage: 654.13 MB
Memory usage: 620.12 MB
Memory usage: 638.74 MB
Memory usage: 703.78 MB
Memory usage: 682.38 MB
Memory usage: 706.70 MB
Memory usage: 669.48 MB
Memory usage: 659.55 MB
Memory usage: 590.86 MB
Memory usage: 815.63 MB
Memory usage: 647.94 MB
Memory usage: 671.21 MB

However, the log information in PsychoPy 2025.1.1 shows that the memory keeps adding up from trial to trial :

Memory usage: 727.04 MB
Memory usage: 1038.37 MB
Memory usage: 1390.97 MB
Memory usage: 1726.13 MB
Memory usage: 2062.73 MB
Memory usage: 2431.46 MB
Memory usage: 2699.79 MB
Memory usage: 3016.97 MB
Memory usage: 3373.02 MB
Memory usage: 3627.20 MB
Memory usage: 3950.56 MB
Memory usage: 4240.99 MB
Memory usage: 4861.85 MB
Memory usage: 5038.25 MB
Memory usage: 5807.51 MB
Memory usage: 6440.71 MB
Memory usage: 6754.29 MB
Memory usage: 7160.10 MB
Memory usage: 8044.99 MB
Memory usage: 9474.24 MB
Memory usage: 9379.82 MB
Memory usage: 9413.09 MB

It seems that the newer version is not cleaning up the garbage. I don’t know if this is a bug. I don’t know enough about Python to evaluate. I just want to put it here in case it is.

1 Like

That’s very interesting. Do you know what components are using the bulk of that memory?

Hello @Bill_Liang

You might want to post your observation to GitHub · Where software is built .

Best wishes Jens

Hi. I think is the visual.ImageStim. I have done what Jens suggested. Instead of creating new boxes and apples each frame, I now create the boxes at the Begin Routine and only create a new apple if participant tap on the target. Then the memory use logging shows the following:

Memory usage: 459.44 MB
Memory usage: 459.17 MB
Memory usage: 502.64 MB
Memory usage: 532.42 MB
Memory usage: 551.23 MB
Memory usage: 565.91 MB
Memory usage: 588.59 MB
Memory usage: 595.58 MB
Memory usage: 618.70 MB
Memory usage: 625.32 MB
Memory usage: 656.70 MB
Memory usage: 685.92 MB
Memory usage: 714.40 MB
Memory usage: 722.30 MB
Memory usage: 748.58 MB
Memory usage: 764.26 MB
Memory usage: 804.85 MB
Memory usage: 799.55 MB
Memory usage: 820.59 MB
Memory usage: 846.36 MB
Memory usage: 873.55 MB
Memory usage: 920.24 MB
Memory usage: 938.46 MB
Memory usage: 944.73 MB
Memory usage: 981.05 MB
Memory usage: 981.29 MB
Memory usage: 1003.17 MB
Memory usage: 1032.04 MB
Memory usage: 1058.02 MB
Memory usage: 1093.07 MB
Memory usage: 1121.87 MB
Memory usage: 1121.11 MB
Memory usage: 1154.17 MB
Memory usage: 1138.30 MB
Memory usage: 1168.88 MB
Memory usage: 1197.62 MB
Memory usage: 1218.28 MB
Memory usage: 1234.20 MB
Memory usage: 1262.69 MB
Memory usage: 1272.21 MB
Memory usage: 1306.35 MB
Memory usage: 1323.23 MB
Memory usage: 1347.66 MB
Memory usage: 1373.66 MB
Memory usage: 1402.04 MB
Memory usage: 1419.03 MB
Memory usage: 1458.56 MB
Memory usage: 1464.93 MB

The overall memory use and the Increment from trial to trial decrease. This suggests that it is the visual.ImageStim holding the memory

Thank you, I will do that.