From: "byroot (Jean Boussier) via ruby-core" <ruby-core@...>
Date: 2023-11-09T08:18:54+00:00
Subject: [ruby-core:115318] [Ruby master Feature#19993] Optionally Free all memory at exit

Issue #19993 has been updated by byroot (Jean Boussier).


> I don't even know if the real-world cases could be ever a problem

Well, for one thing we are currently experiencing a (moderate) issue caused by slow exit in production. We have some code in Pitchfork that basically daemonize and is expected to be "fast" (`Process.wait(fork { fork })`), but sometimes take a handful of seconds because if incremental GC was going on, the `rb_ec_cleanup` will finish GC before exiting.

In this case the numbers you show are no concern to me, but just giving an example of why quick exit is desirable in real world use cases.

So I agree to default to off. The reason I suggest an environment variables (probably under `RUBY_GC_`), is that if I'm not mistaken this would be used by tools such as `ruby_memcheck`, so it's easy for these tools to inject an environment variable. Much easier than a command like flag.

----------------------------------------
Feature #19993: Optionally Free all memory at exit
https://bugs.ruby-lang.org/issues/19993#change-105238

* Author: HParker (Adam Hess)
* Status: Open
* Priority: Normal
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/