Skip to content

How to fix Ray out of memory error in Python?

  • by
  • 3 min read
Top 7 games for kids to learn coding | Candid.Technology

Python is one of the simplest programming languages for beginners to learn and understand. Not only is it easy to learn, but it’s also quite powerful, making it an excellent choice for just about everything from web scraping to building full-scale AI models.

That said, random bugs and errors remain daily challenges that all developers face. In this article, we’re talking about the “ray out of memory” error in Python, its causes and what you can do to fix the problem.

Also read: Fix: Python was not found; run without arguments to install 

What causes this error?

The error originates in the Ray Python framework. Ray is a unified framework for scaling AI and Python applications and includes a core distributed runtime in addition to a library toolkit for simplifying ML compute tasks. 

When the script using Ray runs out of hardware memory on your system, it can trigger this error. Outside of incorrectly set memory thresholds, other popular reasons for the error include the following:

  • Incorrect Ray implementation in Python script. 
  • Memory limits have not been configured. 

Also read: How to fix Fatal error: Python.h: No such file or directory?

How to fix this?

Fixing the error is rather simple and involves declaring a memory threshold for the framework to work with before it maxes out all available memory on your system and crashes with the aforementioned error. The best way to do this is to use the Ray memory monitor to detect and resolve memory issues. 

What is Metadata? Types and benefits | Candid.Technology
Photo by Shahadat Shemul

Once the memory monitor is set up, it periodically checks the memory usage, including the worker heap, object store and raylet. If it exceeds a predetermined threshold, the raylet kills a task or actor process to free up memory and prevent Ray from crashing. 

Alternatively, you can keep track of the running agents yourself and ensure that they’re kept to a minimum so as not to overwhelm your system. Once again, the Ray memory monitor is the easiest way to do this, but if you’re working on a small-scale project, chances are you’ll be quicker just keeping track of running processes yourself. 

Also read: How to fix Mqtt error: Connection refused: Not authorised? 

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: