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.
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.
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.
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.