Till just lately, Ruby lacked a mechanism for detecting native-level memory leaks from within Ruby and native gems. This was because, when Ruby terminates, it doesn't free the objects which can be nonetheless alive or the memory used by Ruby’s virtual machine. It is because the system will reclaim all of the memory used anyway, Memory Wave so it’s just extra work that might make Ruby’s shutdown slower. Nonetheless, this meant that it was impossible to determine whether or not a piece of memory was leaked or was just not cleaned up. This meant that it was onerous to analyze Ruby applications that suffered from memory leaks, causing them to consume growing quantities of memory until the system runs out and terminates the applying. This is undesirable as it's an inefficient use of system sources, which might value cash and performance, and will result in downtime for an internet server. I describe in greater detail in the following section and in another weblog post. Valgrind memcheck to search out memory leaks in native gems.

Valgrind memcheck is a software used to seek out memory leaks in native purposes. Nonetheless, Memory Wave Method we can’t use it immediately on Ruby as a result of Ruby doesn’t free its memory during shutdown, leading Valgrind memcheck to report 1000's of false-constructive memory leaks. Ruby doesn’t free its memory throughout shutdown because the system will reclaim the entire program’s memory after this system has terminated anyway, so explicitly freeing the memory would only make Ruby’s shutdown slower. Since there are tens, if not hundreds, of places where these sorts of "memory leaks" happen in Ruby at shutdown, creating a feature to free all of the memory at shutdown would have been very time-consuming. Ruby or a real memory leak from the native gem. In fact, the heuristic is just not good and could cause false-negatives (i.e. it could actually filter out actual memory leaks). Nokogiri, liquid-c, gRPC, Memory Wave Method and Protobuf. Linux techniques since Valgrind solely runs on Linux.

Which means that we cannot use a faster memory checker like Google’s sanitizers or support other working systems comparable to utilizing the macOS leaks instrument. In 2023, Adam Hess from GitHub collaborated with me to develop a characteristic in Ruby that frees all memory at shutdown. EXIT function which instructs Ruby to free all of its memory at shutdown when the surroundings variable is about. By implementing this with a flag, Ruby can maintain a quick shutdown when this function will not be needed and only free Memory Wave Method at shutdown when the function is enabled. The implementation is pretty easy. Nevertheless, it’s not that easy, since circular dependencies made it difficult. For example, we free Ruby objects before we free the VM as a result of freeing Ruby objects may have the VM to be alive (e.g. executing finalizers), however issues like Threads and the primary Ractor are all Ruby objects, so we cannot free those objects until after most of the VM has been freed.

Edit

Pub: 26 Nov 2025 15:22 UTC

Views: 164