Feature #19993
closedOptionally Free all memory at exit
Description
Add a runtime option allowing Ruby to optionally free all memory at shutdown.
why¶
Today, memory sanitizers are difficult to use with Ruby, since not all memory is freed at shutdown. it is difficult to detect memory leaks or errors in Ruby or in Ruby C extensions when these tools are not available.
While implementing this feature, we were able to identify and fix a number of memory leaks and errors.
https://github.com/ruby/ruby/pull/8556
https://github.com/ruby/ruby/pull/8512
https://github.com/ruby/ruby/pull/8503
https://github.com/ruby/ruby/pull/8487
https://github.com/ruby/ruby/pull/8452
https://github.com/ruby/ruby/pull/8501
https://github.com/ruby/ruby/pull/8481
https://github.com/ruby/ruby/pull/8555
This shows that having access to tools like this can make finding and fixing memory bugs easier.
current progress¶
Today we can allow ruby developers to enable freeing memory at shutdown via the free-at-shutdown
runtime option.
PR: https://github.com/ruby/ruby/pull/8868
running,
without --free-on-shutdown
:
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes -- ./miniruby basictest/test.rb
==573270== LEAK SUMMARY:
==573270== definitely lost: 658,324 bytes in 5,387 blocks
==573270== indirectly lost: 955,708 bytes in 11,957 blocks
==573270== possibly lost: 2,071,096 bytes in 12 blocks
==573270== still reachable: 161,881 bytes in 275 blocks
==573270== suppressed: 0 bytes in 0 blocks
==573270== Reachable blocks (those to which a pointer was found) are not shown.
==573270== To see them, rerun with: --leak-check=full --show-leak-kinds=all
with --free-on-shutdown
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes -- ./miniruby --free-on-shutdown basictest/test.rb
==573306== HEAP SUMMARY:
==573306== in use at exit: 0 bytes in 0 blocks
==573306== total heap usage: 43,643 allocs, 43,643 frees, 29,222,534 bytes allocated
==573306==
==573306== All heap blocks were freed -- no leaks are possible
future plans¶
- Continue improving memory safety
- add
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes -- ./miniruby --free-on-shutdown basictest/test.rb
to CI - Allow C extensions to do a "optional destruct for memory safety" so they can leverage the same memory sanitizer tools.