R Cannot Allocate Vector of Size

Sheeraz Gul Nov 15, 2022
  1. R Cannot Allocate Vector of Size
  2. Solution 1: Garbage Collection Using gc()
  3. Solution 2: Increase Memory Limit
R Cannot Allocate Vector of Size

This tutorial demonstrates the error r cannot allocate vector of size.

R Cannot Allocate Vector of Size

The cannot allocate vector of size error occurs in R when we are trying to deal with a large amount of data. This error is not a syntax error but a logical one that occurs when dealing with an extremely large amount of data because it uses a large amount of virtual memory.

Most of the time, this error occurs when the data is loaded from an external source like a package or any other file. When the compiler cannot control the object’s size, it will throw the error cannot allocate vector of size.

The error cannot allocate vector of size occurs when we create a vector of the object or load a function. This error can also occur with smaller objects when we have many smaller objects.

The error has two solutions, the gc() and memory.limit(), where the gc() doesn’t work in every situation. We demonstrate both methods in this tutorial.

Solution 1: Garbage Collection Using gc()

Garbage collection is the first solution that comes to mind; there is an error cannot allocate vector of size, and it works in many situations. But the GC will not work when the amount of data is way more.

The method gc() is used for the garbage collection in R. Let’s try an example that throws this error and then try to solve it with gc().

demo <- rnorm(5000000000)

The code above will create a vector from randomly distributed values, and according to our system, it should throw the error cannot allocate vector of size. See the output:

Error: cannot allocate vector of size 37.3 Gb
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'Matrix'

As we can see, the compiler requires the memory of the error: cannot allocate vector of size 37.3 Gb, which is not possible for our system. Not let’s try to run GC and then this code again.

# Run GC
gc()
# Run the above code
demo <- rnorm(5000000000)

The code above will perform the GC and then try to re-run the code; if after GC, there is a memory of 37.3 GB, then the error will be solved; otherwise, it will still throw the error. See the output:

          used (Mb) gc trigger  (Mb) max used (Mb)
Ncells 1004541 53.7    2078938 111.1  1255027 67.1
Vcells 2577662 19.7    8388608  64.0  2836345 21.7

Error: cannot allocate vector of size 37.3 Gb

As we can see, it still throws the same error because the GC is clearing a small amount of memory, which needs more. So the gc() only work for small situations.

Solution 2: Increase Memory Limit

Some time ago, we could increase the memory limit in R using the memory.limit() with the size attribute, solving the error cannot allocate vector of size even in a large data situation. But this method is no longer supported in R, and as per R documentation, we can increase the memory from the OS option.

Follow the step-by-step process to increase memory which will solve the error cannot allocate vector of size.

  • First of all, make sure the RStudio is closed.
  • Now find the R program shortcut and right-click on it to go to the Properties.

    R Properties

  • Once you enter the Properties, go to the Shortcut tab and look for the Target field.

    R Properties Target Field

  • Add the following line at the end of the target field.
    --max-mem-size=45000M--max-vsize=45000M
    

    R Properties Increase Memory

  • The above line will increase the memory to 45000 MBs, which is greater than 37.3 GB. The vsize in the above line identifies the vector size.
  • After setting the maximum memory and maximum vector size, open the RStudio and run the following code:
    demo <- rnorm(5000000000)
    

Now, this code will throw and create a vector named demo. The size 37.3 GB is too large; it might hang your computer if it cannot support the memory.

But whenever the error cannot allocate vector of size occurs, we can use this method to solve it.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - R Error