MemoryError: Memory allocation failed, allocating 1024 bytes

Hi,

I’m using an openMV M7 to track a line using color detection. So basically, i’m setting the camera, all my com stuff and calling :

img = sensor.snapshot()

My application crashes sometimes with a pop-up indicated only “MemoryError: Memory allocation failed, allocating 1024 bytes” when calling this line that evaluates the current luminosity :

gs_mean = img.get_histogram(roi=roi_bottom).get_statistics().mean()

I was wondering the memory capabilities of the board, could anyone tell me more about this subject ? How much RAM is available ? How big is the frame buffer ?

thanks !

Hi, this is an error with the MicroPython Heap. Get histogram returns a rather large array of numbers which you throw away. But, those numbers have to be put on the heap in a linear array of floats. There are 3 arrays of 1024B each. So, it can be hard to fit them all sometimes once the heap gets fragmented.

Note that you can call get_statistics() directly on the image. Do that to avoid the get_histogram call. This shouldn’t fail then. get_statistics will also support the ROI argument.

Please see the M7 product page for the RAM under the specs tab.

Also, you can pass a parameter to get histogram to reduce the number of bins if you are still having issues. This will reduce the RAM requirement.

I really appreciate your answer thank you.

To people looking for the RAM specs, here is what is mentioned in the CAM M7 datasheet (RAM Layout) :

  • 128KB .DATA/.BSS/Heap/Stack
  • 384KB Frame Buffer/Stack
    (512KB Total)

[quote=“kwagyeman”]
Note that you can call get_statistics() directly on the image. Do that to avoid the get_histogram call. This shouldn’t fail then. get_statistics will also support the ROI argument.
[/quote]

OK I will definitively use get_statistics() directly on an image.
I’ll come back to you if I encounter any problem.

Best regards,
MG.

Note there’s also a 128KBs Tightly Coupled Memory (TCM) memory that we’re also using. Also That’s not the actual memory layout we’re using, it depends on the cam. For M7 the data, bss, heap and stack sections (as well as some buffers) are placed in the TCM. The framebuffer and some memory allocator use the main SRAM.

Edit: you mentioned the 128KBs already.