Why am I receiving an "Unable to allocate memory" error when acquiring an image from my camera?

I am attempting to acquire an image from my Logitech Webcam (using the "winvideo" adaptor) in R2021a, but I am receiving the following error message:
Unable to allocate memory for an incoming image frame due to insufficient free physical memory.
The output of my "memory" command is as follows:
>> memory
Maximum possible array: 3946 MB (4.138e+09 bytes) *Memory available for all arrays: 3946 MB (4.138e+09 bytes) *Memory used by MATLAB: 2684 MB (2.814e+09 bytes)Physical Memory (RAM): 16112 MB (1.689e+10 bytes)
* Limited by System Memory (physical + swap file) available.
What can I do to resolve this error?

 Accepted Answer

Given the output of the "memory" command, this is likely an issue caused by a lack of available RAM on the machine. If we look at the memory usage, we can see that the machine has allocated about 12 GB of the total 16 GB to other programs, and of the 4 GB that MATLAB can use, it is already using about 2.6 GB. This leaves a relatively small 1.4 GB of memory left for remaining MATLAB computations and data storage.
There are a couple ways to try and alleviate this issue:
  • Close all other programs running on the machine that are not required for the image acquisition and analysis process. This will free up memory that MATLAB can then take advantage of.
  • You can "reserve" some memory for MATLAB to use by pre-allocating a matrix that will eventually store the image. At the start of the program, you can allocate a matrix of zeros with the same size as the incoming image and then you can overwrite this matrix with the incoming image data when an image is captured. For example, if you are expecting a 1920x1080 RGB image, allocate a matrix using
>> zeros(1920, 1080, 3)
As long as there is enough RAM available at the start of the program to allocate this matrix of zeros and you never delete this matrix, in effect, MATLAB will be reserving enough RAM to store the incoming image data.
  • Clear variables from the MATLAB workspace that are not being used for the image acquisition and analysis, which will increase the available memory.
  • Reduce the amount of memory that is being used by variables in your workspace by using the smallest data type possible. For example, using matrices of data type single instead of the default data type double.
  • Install more RAM on your machine or use a machine with more physical RAM installed if you need to have many other programs running at the same time.

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!