Added new swig_gc.rb file to obtain easy stats

from ruby's GC.
Updated newobject1_* tests to use it.



git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9695 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Gonzalo Garramuno 2007-04-28 17:29:11 +00:00
commit fd79f15521
3 changed files with 47 additions and 8 deletions

View file

@ -18,12 +18,18 @@
#
require 'swig_assert'
require 'swig_gc'
require 'newobject1'
include Newobject1
stats = nil
stats = GC.stats(stats, Foo)
100.times { foo1 = Foo.makeFoo; foo2 = foo1.makeMore }
stats = GC.stats(stats, Foo)
swig_assert( 'Foo.fooCount == 200', "but is #{Foo.fooCount}" )
GC.start
stats = GC.stats(stats, Foo)
swig_assert( 'Foo.fooCount <= 2', "but is #{Foo.fooCount}" )

View file

@ -12,18 +12,17 @@
#
require 'swig_assert'
require 'swig_gc'
require 'newobject2'
include Newobject2
stats = nil
stats = GC.stats(stats, Foo)
100.times { foo1 = makeFoo }
stats = GC.stats(stats, Foo)
swig_assert( 'fooCount == 100', "but is #{fooCount}" )
GC.start
swig_assert( 'fooCount == 1', "but is #{fooCount}" )
stats = GC.stats(stats, Foo)
swig_assert( 'fooCount <= 1', "but is #{fooCount}" )
@foos = []
100.times { @foos << makeFoo }
swig_assert( 'fooCount == 101', "but is #{fooCount}" )
GC.start

View file

@ -0,0 +1,34 @@
#!/usr/bin/env ruby
#
# Put script description here.
#
# Author::
# Copyright::
# License:: Ruby
#
#
# VERY nice function from Robert Klemme to check memory leaks
# and check on what GC has collected since last call.
#
module GC
def self.stats(last_stat = nil, klass = nil)
stats = Hash.new(0)
ObjectSpace.each_object {|o| stats[o.class] += 1}
if klass
v = stats[klass]
printf "%-30s %10d", klass.to_s, v
printf " | delta %10d", (v - last_stat[klass]) if last_stat
puts
else
stats.sort {|(k1,v1),(k2,v2)| v2 <=> v1}.each do |k,v|
printf "%-30s %10d", k, v
printf " | delta %10d", (v - last_stat[k]) if last_stat
puts
end
end
stats
end
end