Skip to content

Instantly share code, notes, and snippets.

@mzsanford
Created May 21, 2013 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsanford/5622541 to your computer and use it in GitHub Desktop.
Save mzsanford/5622541 to your computer and use it in GitHub Desktop.
Sinatra memory usage - part 2 (dup change)
∴ ruby subclass.rb
== Sinatra/1.4.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
---- Init myApp
-- Setting up @array id 70222006630940
-- Starting GC
---- Waiting in memory (93296): #<MyApp:0x007fbbab031538 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fbbab031308 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
-- Setting up @array id 70222007258980
-- Starting GC
****** GC Array(70222006630940) Starting
---- Waiting in memory (93296): #<MyApp:0x007fbbab031538 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fbbab031308 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
-- Setting up @array id 70222002441220
-- Starting GC
---- Waiting in memory (93296): #<MyApp:0x007fbbab031538 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fbbab031308 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
****** GC Array(70222007258980) Starting
^C>> Stopping ...
== Sinatra has ended his set (crowd applauds)
****** GC Array(70222002441220) Starting
# ruby-head only
require 'sinatra/base'
require 'objspace'
class MyApp < Sinatra::Base
def initialize(*args)
puts "---- Init myApp"
super(*args)
end
def call(env)
# default #call uses:
# dup.call!(env)
call!(env)
end
get '/' do
# Changed to member variable.
@array = []
10000.times do
@array << Object.new
end
puts "-- Setting up @array id #{@array.object_id}"
ObjectSpace.define_finalizer(@array, proc {|id| puts "****** GC Array(#{id}) Starting" })
@array.size.to_s
end
after do
puts "-- Starting GC"
ObjectSpace.each_object(MyApp) do |obj|
sz = ObjectSpace.reachable_objects_from(obj).map{|o| ObjectSpace.memsize_of(o) }.inject{|sum, x| sum + x }
puts " ---- Waiting in memory (#{sz}): #{obj.inspect.to_s.slice(0,150)}"
end
GC.start
end
end
MyApp.run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment