While working on code to test Puma’s performance, I noticed a memory leak using WSL2 Ubuntu 20.04 and Ruby master. The leak seemed proportional to the number of requests made, and independent of the response size, either headers or body. Locally, I was running up to 2 million requests. The code used @ioquatix’s version of wrk and smem for measuring memory use. I ran GC.start and GC.compact before checking memory. The Puma configuration was using fork.
I’ve got a lot of Ruby versions on Windows, but haven’t created the same setup for Ubuntu. So, I took the above code and ran it on GitHub Actions Ubuntu 20.04. Ruby 2.5.9, 2.7.4, and 3.0.2 did not have the memory leak, but master had the same leak I saw locally.
Puma and one of its dependencies (nio4r) are both extension gems. Has there been an ABI change that might affect the code?
Assuming this is caused by c code, Puma's only c code when running the test is the http parser, which has been relatively static, and is derived from the old Mongrel parser. I'm not familiar with the Nio4r code used...
I can confirm this, and also that the leak stops by reverting 0ef2923c2b9 (#16996).
$ ./miniruby bug18134.rb
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a116 12366 10224 50636 7388 poll_sched b6e3cd28 S ./miniruby
Duped hash 500000 times
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a116 12366 10224 168728 125492 poll_sched b6e3cd28 S ./miniruby
$ ./miniruby.revert bug18134.rb
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a116 12374 10224 50636 7388 poll_sched b6e34d28 S ./miniruby.revert
Duped hash 500000 times
USER PID PPID VSIZE RSS WCHAN PC NAME
u0_a116 12374 10224 51608 8376 poll_sched b6e34d28 S ./miniruby.revert
BTW, the first line of the repro should be something like: