class - Anonymous modules and classes garbage collection in Ruby -


i'd know why following code apparently doesn't garbage collect anonymous modules supposedly not referenced anywhere anymore (not extended/included, not named, containing array set nil).

i'd appreciate if clarify what's going on under hood relatively simple/general programming words. there special ruby way achieve ? can't anonymous modules/classes garbage collected no matter ? or mislead memory stats got ?

note : i'm using ruby 1.9.3 ; don't know if ruby 2.x change @ all... note 2 : result same whether or not module defines foo method

thanks in advance.

puts("initial object space objects : #{objectspace.count_objects}")   = 100000 ms = []  i.times     ms << module.new         def foo()              puts('foo method called')         end     end end  puts("#{i} modules created") puts("object space objects : #{objectspace.count_objects}")  ms = nil objectspace.garbage_collect  puts("#{i} modules garbage collected") puts("waiting end program")  stop = gets puts("final object space objects : #{objectspace.count_objects}") 

i "apparently doesn't garbage collect" because os task manager doesn't show reduction in memory usage process, , calling objectspace.count_objects yields following, read (wrongly ?) : no memory used modules has not been freed.

initial object space objects : {:total=>14730, :free=>251, :t_object=>8, :t_class=>542, :t_module=>21, :t_float=>7, :t_string=>6349, :t_regexp=>24, :t_array=>1009, :t_hash=>14, :t_bignum=>3, :t_file=>10, :t_data=>408, :t_match=>108, :t_complex=>1, :t_node=>5956, :t_iclass=>19}

100000 modules created

object space objects : {:total=>311794, :free=>59829, :t_object=>6, :t_class=>542, :t_module=>100021, :t_float=>7, :t_string=>3332, :t_regexp=>22, :t_array=>23868, :t_hash=>10, :t_bignum=>3, :t_file=>3, :t_data=>100324, :t_complex=>1, :t_node=>23807, :t_iclass=>19}

100000 modules garbage collected waiting end program

final object space objects : {:total=>311794, :free=>107155, :t_object=>6, :t_class=>542, :t_module=>100021, :t_float=>7, :t_string=>3335, :t_regexp=>22, :t_array=>203, :t_hash=>10, :t_bignum=>3, :t_file=>3, :t_data=>100324, :t_complex=>1, :t_node=>143, :t_iclass=>19}

calling gc.start or objectspace.garbage_collect does not mean garbage collection performed. it's hint ruby runtime.

on platforms isn't possible ruby runtime initiate garbage collection, because garbage collector isn't part of ruby runtime, e.g on jruby or ironruby.

in general, ruby runtime decide on own when perform garbage collection. usually, that's when runs out of memory. 100000 modules aren't that big, there's no need perform gc cycle.

also, ruby implementations never release memory os, after gc cycle.

so, fact objectspace doesn't shrink doesn't mean have memory leak. mean ruby runtime didn't yet deem necessary run gc cycle.

also note if run code irb, pry, ide console or other non-standard environment, may case those hold on of modules. example, pry stores results of last 100 commands in history array. (try evaluating _out_[5] in pry, after entering example program.)


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -