From: timr on 21 May 2010 02:30 I was playing around looking at ObjectSpace in irb and was astounded at how many string objects there are. Why are there 57,000+ String objects?!? hash = {} ObjectSpace.each_object.collect{|t| t.class}.uniq.each{|cla| hash[cla] = ObjectSpace.each_object(cla).to_a.length} hash {Tiger=>1, Gem::SourceIndex=>1, Gem::Version=>601, String=>57656, RubyToken::TkNL=>1, Gem::Dependency=>206, Proc=>127, NoMemoryError=>1, UnboundMethod=>1, IRB::WorkSpace=>1, Rational=>10, Mutex=>1, Thread=>1, IO=>5, Object=>64965, Binding=>3, Array=>3761, Float=>18, OptionParser::Switch::NoArgument=>2, IRB::Notifier::CompositeNotifier=>1, Range=>51, Bignum=>2, fatal=>1, OptionParser::OptionMap=>2, Gem::CommandManager=>1, IRB::Locale=>1, RubyLex::TerminateLineInput=>1, Date::Infinity=>2, Gem::Requirement=>554, SystemStackError=>1, Module=>495, Hash=>130, YAML::Syck::Resolver=>2, ThreadGroup=>1, Gem::Specification=>173, IRB::SLex=>1, Gem::GemPathSearcher=>1, File=>2, Class=>438, IRB::Notifier::LeveledNotifier=>4, RubyToken::TkLPAREN=>1, OptionParser::List=>1, OptionParser::CompletingHash=>1, RubyToken::TkRBRACE=>1, Enumerable::Enumerator=>10, Method=>1, IRB::ReadlineInputMethod=>1, IRB::StdioOutputMethod=>2, RubyLex=>1, IRB::SLex::Node=>78, Gem::ConfigFile=>1, MatchData=>4, IRB::Context=>1, Regexp=>353, IRB::Notifier::NoMsgNotifier=>1, IRB::Irb=>1, Gem::Version::Part=>481, Time=>176} And if I do: ObjectSpace.each_object(String).each{|str| hash[str.object_id] = str} I find that most of these strings appear to be paths to ruby files. I am curious if everyone has a similarly large set of string objects in their ObjectSpace, or is this a result of my personalized setup?
From: Josh Cheek on 21 May 2010 04:13 [Note: parts of this message were removed to make it a legal post.] On Fri, May 21, 2010 at 1:35 AM, timr <timrandg(a)gmail.com> wrote: > I was playing around looking at ObjectSpace in irb and was astounded > at how many string objects there are. Why are there 57,000+ String > objects?!? > > hash = {} > ObjectSpace.each_object.collect{|t| t.class}.uniq.each{|cla| hash[cla] > = ObjectSpace.each_object(cla).to_a.length} > hash > > {Tiger=>1, Gem::SourceIndex=>1, Gem::Version=>601, String=>57656, > RubyToken::TkNL=>1, Gem::Dependency=>206, Proc=>127, NoMemoryError=>1, > UnboundMethod=>1, IRB::WorkSpace=>1, Rational=>10, Mutex=>1, > Thread=>1, IO=>5, Object=>64965, Binding=>3, Array=>3761, Float=>18, > OptionParser::Switch::NoArgument=>2, > IRB::Notifier::CompositeNotifier=>1, Range=>51, Bignum=>2, fatal=>1, > OptionParser::OptionMap=>2, Gem::CommandManager=>1, IRB::Locale=>1, > RubyLex::TerminateLineInput=>1, Date::Infinity=>2, > Gem::Requirement=>554, SystemStackError=>1, Module=>495, Hash=>130, > YAML::Syck::Resolver=>2, ThreadGroup=>1, Gem::Specification=>173, > IRB::SLex=>1, Gem::GemPathSearcher=>1, File=>2, Class=>438, > IRB::Notifier::LeveledNotifier=>4, RubyToken::TkLPAREN=>1, > OptionParser::List=>1, OptionParser::CompletingHash=>1, > RubyToken::TkRBRACE=>1, Enumerable::Enumerator=>10, Method=>1, > IRB::ReadlineInputMethod=>1, IRB::StdioOutputMethod=>2, RubyLex=>1, > IRB::SLex::Node=>78, Gem::ConfigFile=>1, MatchData=>4, > IRB::Context=>1, Regexp=>353, IRB::Notifier::NoMsgNotifier=>1, > IRB::Irb=>1, Gem::Version::Part=>481, Time=>176} > > > And if I do: > > ObjectSpace.each_object(String).each{|str| hash[str.object_id] = str} > > I find that most of these strings appear to be paths to ruby files. I > am curious if everyone has a similarly large set of string objects in > their ObjectSpace, or is this a result of my personalized setup? > > Using your code, in irb: MRI 1.8.7 => 36715 MRI 1.9.1 => 3752 Rubinius 1.0 => 4797 MacRuby 0.6 => 3013 And your code didn't work in JRuby 1.5.0, or MRI 1.8.6 However, outside of irb, using ruby -e "hash = {} ; ObjectSpace.each_object.collect{|t| t.class}.uniq.each{|cla| hash[cla] = ObjectSpace.each_object(cla).to_a.length} ; p hash[String]" I got: MRI 1.8.7 => 126 MRI 1.9.1 => 1708 Rubinius 1.0 => 3424 MacRuby 0.6 => 1120
From: Robert Dober on 21 May 2010 04:46 On Fri, May 21, 2010 at 8:35 AM, timr <timrandg(a)gmail.com> wrote: > ObjectSpace.each_object(String).each{|str| hash[str.object_id] = str} > > I find that most of these strings appear to be paths to ruby files. I > am curious if everyone has a similarly large set of string objects in > their ObjectSpace, or is this a result of my personalized setup? Sorry really did not see what you were looking for, somehow missed your post, my bad. I checked and my string path objects are from $:, the global include path. Cheers R. > > -- The best way to predict the future is to invent it. -- Alan Kay
From: Robert Dober on 21 May 2010 04:39 > jruby -X+O -ve 'p ObjectSpace.each_object(String).count' > jruby 1.5.0.RC3 (ruby 1.8.7 patchlevel 249) (2010-05-05 6586) (OpenJDK > Client VM 1.6.0_0) [i386-java] > 519 and 553 for 1.5.0
From: Robert Klemme on 21 May 2010 05:51
2010/5/21 timr <timrandg(a)gmail.com>: > I was playing around looking at ObjectSpace in irb and was astounded > at how many string objects there are. Why are there 57,000+ String > objects?!? It would be interesting to know what code you executed *before* you created your stats. Note that this includes any libraries required and files loaded. Another question is: is there a problem? Is your program slow or running out of memory? If not, why care? Eventually all these objects will be GC'ed - unless you have a leak of course. Btw: 11:47:36 Temp$ cat c.rb cnt = Hash.new 0 ObjectSpace.each_object(Object) do |o| cnt[o.class] += 1 end cnt.sort_by {|k,v| -v}.each do |cl,count| printf "%6d %s\n", count, cl end 11:49:54 Temp$ allruby c.rb CYGWIN_NT-5.1 padrklemme1 1.7.5(0.225/5/3) 2010-04-12 19:07 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] 179 Class 167 String 16 Module 6 Array 5 Float 3 Object 3 IO 1 File 1 Binding 1 Hash 1 SystemStackError 1 NoMemoryError 1 Bignum 1 fatal 1 ThreadGroup 1 Thread ======================================== ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-cygwin] 1659 String 201 Class 83 Encoding 48 RubyVM::InstructionSequence 18 Module 14 Array 10 Hash 10 Regexp 5 Bignum 5 Float 3 IO 2 Object 2 RubyVM::Env 1 Binding 1 ThreadGroup 1 Thread 1 RubyVM 1 NoMemoryError 1 Complex 1 SystemStackError 1 ARGF.class 1 Mutex 1 Data 1 File 1 fatal ======================================== jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.6.0_20) [x86-java] c.rb:5:in `each_object': ObjectSpace is disabled; each_object will only work with Class, pass -X+O to enable (RuntimeError) from c.rb:5 11:50:10 Temp$ Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ |