Issue #15121 has been reported by william101 (William Tabi).

----------------------------------------
Bug #15121: Memory Leak on rb_id2name(SYM2ID(sym))
https://bugs.ruby-lang.org/issues/15121

* Author: william101 (William Tabi)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
@ohler55 mentioned in https://github.com/ohler55/oj/issues/501 that calling `rb_id2name(SYM2ID(sym))` seems to lock symbols in memory but I couldn't find any issue open for that. So I'm just opening one just in case, but pls close if this is a dupe. 

I created a sample C extension to reproduce this

~~~ c
#include "extconf.h"
#include <stdlib.h>
#include <stdio.h>
#include <ruby.h>

VALUE
rb_leak_sym(VALUE self, VALUE argument1) {
    const char	*sym = rb_id2name(SYM2ID(argument1));
    return Qnil;
}

void Init_testsym()
{
    rb_define_global_function("leak_sym", rb_leak_sym, 1);
}
~~~

We can see it leaking memory with this snippet
~~~ ruby
require "testsym"
require "objspace"

def record_allocation
  GC.start
  GC.start

  puts "Before - Objects count: #{ObjectSpace.each_object.count}"
  puts "Before - Symbols count: #{Symbol.all_symbols.size}"

  yield

  GC.start
  GC.start

  puts "After - Objects count: #{ObjectSpace.each_object.count}"
  puts "After - Symbols count: #{Symbol.all_symbols.size}"
end

def leak_symbols
  1_000_000.times.each { |i| leak_sym("string_number_#{i}".to_sym) }
end

record_allocation do
  leak_symbols
end

~~~

Output:
~~~
$ ruby -v test.rb
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]
Before - Objects count: 8784
Before - Symbols count: 3063
After - Objects count: 2008786
After - Symbols count: 1003063
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>