How To Fix “Error: vector memory exhausted (limit reached?)” In R

Error vector memory exhausted (limit reached)

Memory management is an important aspect of R programming. Problems like the Error: vector memory exhausted (limit reached?) can happen quite often when your system can’t match the hardware requirements of your R programs.

Read on to find out the root cause of this program and how to resolve it.

What causes”Error: vector memory exhausted (limit reached?)”

One of the first things R scientists and engineers must know about this is an in-memory language.

This means it always tries to host all objects in the system memory. This design approach helps boost flexibility and performance. As you might already know, objects stored in RAM are much faster to access and read from compared to traditional storage options like HDD or even SSD.

However, your R programs may eat up a large chunk of your system’s memory when they have complex algorithms and deal with big datasets.

On top of that, R uses garbage collection instead of immediately dropping objects from the memory when they are no longer in use. This collector only runs on an occasional basis and checks for data that aren’t being referenced anymore. It isn’t until that point that those objects are removed from the memory to create more room for your program.

When R can’t get more memory allocated to your program, it will print out this message:

Error: vector memory exhausted (limit reached?)

Solutions

Switch To The 64-Bit Build

R’s developers produce both 32- and 64-bit builds. Most 64-bit operating systems (Windows, macOS, and Linux) can run either variant. However, there is a massive difference in memory limitations between those builds.

When you run a 32-bit R installation, the system imposes a limit (usually less than 4GB) on the maximum memory it can use. This is a severe limitation that doesn’t go away even when you have a 64-bit machine with plenty of RAM. In fact, most 32-bit Windows versions can only allocate 2GB to 32-bit applications by default. 

If you have a 64-bit computer, install the 64-bit versions of your operating system and R to remove these memory restrictions.

You can learn more about your current R installation with the version command:

> version
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu 

 For instance, these lines indicate that you have a 64-bit R build on a 64-bit Linux system.

Increase The Memory Limit On Windows

You can use the memory.limit() function to set the new limit on memory allocation.

This is only applicable when you have 32-bit R builds on Windows. There is no need to do this with 64-bit builds on Windows or other platforms as they don’t have such restrictions on the amount of memory R can use.

Syntax:

memory.limit(size = NA)

NA is the value of maximum obtainable RAM in MB, and it should not exceed 4095. You can also set the environment variable R_MAX_MEM_SIZE to this value to achieve the same effect.

Install More RAM

If you can switch your program to a system with a higher memory capacity, you can upgrade the RAM of your existing system.

High-performance PCs often have 16, 32, or even 64GB of RAM these days. The limitation depends on the number of available RAM slots on your computer and how much RAM you can install to each of them.

Remember that RAM sticks should be installed in pairs and should have the same specifications, so your system can have optimal performance. Check the RAM’s compatibility with the motherboard and CPU, or your expensive purchases might go to waste.

You can install RAM sticks yourself or ask for help from a technician.

Summary

The Error: vector memory exhausted (limit reached?) appears when your R program can’t find any more memory for its operation. You can get more RAM and use 64-bit builds to make more memory available to it.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *