The “cannot allocate vector of size” error message is a memory allocation problem that can arise when dealing with a large amount of data. This does not necessarily involve a coding mistake in your R script but there are situations where there is a coding solution. The challenge here is not so much a matter of finding the source of the problem but of finding the best solution to it.
The circumstances of this error.
The “cannot allocate vector of size” memory error message occurs when you are creating or loading an extremely large amount of data that takes up a lot of virtual memory usage. When dealing with such large datasets it is possible to actually run out of usable memory. It is most likely to happen when a data set is loaded in from an external source such as a package or another type of file. This is because you cannot control the size of an object contained in a source that you did not create. It is most likely to occur when accessing the function to load or create a vector or other data set. They can also occur with smaller objects if you have an extremely large number of objects in your program.
What is causing this error?
The cause of the “cannot allocate vector of size” error message is a virtual memory allocation problem. It mainly results from large objects who have a vector size that exceeds the memory limit for the project. It can also occur because you have a large number of individual objects that together exceed the maximum memory limit for the project. Now, this can be a matter of running out of physical memory usage space on your computer or maximum memory size limits set by the software this is most likely to occur if you are running the 32-bit RStudio on a 64-bit machine. If you have a fairly new computer it is highly unlikely that you will run into this object error as a result of running out of physical memory unless you are working with extremely large amounts of data in one character string or calculation.
How to fix this error.
The “cannot allocate vector of size” memory issue error message has several R code solutions. The best thing about these solutions is that none of them is overly complicated, most are a simple single process that is easy to do in your R script.
- The simplest solution is to avoid using overly large objects or excessively large numbers of them in one program or R session, for example try removing unneeded objects from your calculation to better fit within your total allocation of memory.
- When reading in an external file enclose the read() function inside a subset() function resulting in the format of subset(read.(“filename”, header = TRUE), select = c(columns to be kept)) this will reduce the size of individual objects being created by removing unwanted columns.
- You can clear out unneeded objects using the rm() function.
- You can clean out address space using the gc() function.
- You can also test for a limit to the memory using the memory.limit() and set a higher value with the format of memory.limit(size=number).
- Shut down any programs you are not using to clear up extra space.
Finding the best process to fix this error may take some trial and error but they are not hard or time-consuming. While this error message is not a coding problem it is still not difficult to fix within your R session.
R Error cannot allocate vector of size