Skip to content

Instantly share code, notes, and snippets.

@octplane
Created February 22, 2013 10:33
Show Gist options
  • Save octplane/5012452 to your computer and use it in GitHub Desktop.
Save octplane/5012452 to your computer and use it in GitHub Desktop.
Stress Test V8 to exhibit memory usage. Uncomment line 5 to demonstrate that SetFlagsFromString has actual impact on the V8 runtime.
require 'rubygems'
require 'handlebars'
require 'v8'
# V8::C::V8.SetFlagsFromString("--max-old-space-size=200")
@heap_stats_methods = [:total_heap_size, :total_heap_size_executable, :used_heap_size, :heap_size_limit]
@st = V8::C::HeapStatistics.new
def dump_stats
V8::C::V8.GetHeapStatistics(@st)
@heap_stats_methods.each do |m|
v = @st.send(m)/1_000_000
puts "#{m} : #{"%05.2f" % v}MB"
end
end
puts "starting up"
c = Handlebars::Context.new
1.upto(100) do |i|
t = "pouet#{i} {{gna}} "+"this is a dummy text."*1_000
d = {gna: "chlu"}
c.compile(t*1_000).call(d)
if i % 10 == 0
print "."
$stdout.flush
end
if i % 10 == 1
puts "At #{i}"
dump_stats
GC.start()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment