Calling on the GC after Rubygems
Calling GC.start immediately after requiring rubygems cut the raw string size down to 45%. heh.
Please note (update): Eric has said (see comments) that this is better in the latest build of rubygems, and that is now out (0.9.5). Re-running the test (I have 98 gems installed at present), I still see a 75% drop in raw in-memory string size GC’ing after rubygems has loaded. I suspect this has little to do with rubygems itself, but more the manner in which ruby caches file data as it is loaded. That and the fact that the ruby GC has no reason to run automatically at this stage (It is programmed not to, which is another discussion already covered by _why). Whether or not we should care is a whole other kettle of fish. I may have a look into this in the future, as there are actually good points on several sides, from pre-optimisation, to platform optimisation, to actual speed (sometimes it may not matter), to memory fragmentation. I don’t have an answer, and likely no one does right now, and certainly not for all cases. This is information only, it might help someone.
[source:ruby]
RAW = true
def write_strings_to fn
open(fn, ‘w+’) do |f|
ObjectSpace.each_object do |o|
f.send((RAW ? :print : :puts), o) if o.class == String
end
end
end
file = %w[strings_no_gems.log strings_gems_no_gc.log strings_gems.log]
GC.start
write_strings_to file[ 0 ]
require ‘rubygems’
write_strings_to file[ 1 ]
GC.start
write_strings_to file[ 2 ]
last = File.size file[ 0 ]
file.each do |fn|
cur = File.size(fn).to_f
pcge = cur / last * 100
puts “#%26s: %15d bytes, change: %8d%” % [fn, cur, pcge]
last = cur
end
[/source]
The raw byte sizes fluctuated a small amount, but in general, the percentage values stayed the same.
# out_strs.rb # strings_no_gems.log: 1707 bytes, change: 100% # strings_gems_no_gc.log: 99578 bytes, change: 5833% # strings_gems.log: 45533 bytes, change: 45%
I have around 150 gems installed, and requiring rubygems grows the ruby process by around 5mb on my machine.
This is unnecessary in the next version of rubygems (currently out in beta).