Depending on how I generate a string Ruby will release the memory to the OS or it won't. The first test code will take up about 235MB
size = 2**22
string = '!@#$%^&*()-+~`a1234567890abcdefghijklmnopqrstuvwxyz' * size
puts 'Sleeping...'
sleep(5)
string = nil
GC.start
puts 'Just sitting here..'
gets.chomp
After GC.start
is called, memory used by the test will shrink back to a few kilobytes. But if I run the same test with string = (0...size).map { (65 + rand(26)).chr }.join
, memory will shoot up to 250MB and memory use will actually increase to 290MB after calling GC.start
.
EDIT: I'm using Ruby 1.9.3-p448 as the project I'm working on requires it. Although I'll test it on Ruby 2.2 and come back with results.
EDIT 2: Running the test code in Ruby 2.1 (Ruby 2.2 wasn't available in RVM and I just wanted to run the test quickly) gave similar results. Memory still didn't decrease to a reasonable state. It went from 234MB BGCs (before GC.start) to 197MB AGCs. Note: the memory sizes were different because I ran it on a different machine but the specific sizes don't matter just the relative increases and decreases (or non-decreases).