PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/ruby/swig_gc.rb

#
Ruby | 70 lines | 30 code | 9 blank | 31 comment | 4 complexity | 3951e3606361e692959bbcfee7b7354f MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #!/usr/bin/env ruby
  2. #
  3. #
  4. # VERY nice function from Robert Klemme to check memory leaks
  5. # and check on what GC has collected since last call.
  6. #
  7. # Usage can be:
  8. #
  9. # require 'swig_gc'
  10. #
  11. # GC.stats
  12. # # do some stuff..
  13. # GC.start # collect and report stats
  14. # # do some more...
  15. # GC.stats # just report stats
  16. #
  17. # or:
  18. #
  19. # require 'swig_gc'
  20. #
  21. # GC.track_class = String # track just String classes
  22. # GC.stats
  23. # # do some stuff..
  24. # GC.start # collect and report stats
  25. # # do some more...
  26. # GC.stats # just report stats
  27. #
  28. #
  29. #
  30. #
  31. #
  32. module GC
  33. class << self
  34. attr :last_stat
  35. attr_accessor :track_class
  36. alias :_start :start
  37. def start
  38. _start
  39. stats if $VERBOSE
  40. end
  41. def stats
  42. stats = Hash.new(0)
  43. ObjectSpace.each_object {|o| stats[o.class] += 1}
  44. if track_class
  45. v = stats[track_class]
  46. printf "\t%-30s %10d", track_class.to_s, v
  47. if last_stat
  48. printf " | delta %10d", (v - last_stat[track_class])
  49. end
  50. puts
  51. else
  52. stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v|
  53. printf "\t%-30s %10d", k, v
  54. printf " | delta %10d", (v - last_stat[k]) if last_stat
  55. puts
  56. end
  57. end
  58. last_stat = stats
  59. end
  60. end
  61. end