Skip to content

Instantly share code, notes, and snippets.

@mzsanford
Last active December 17, 2015 14:09
Show Gist options
  • Save mzsanford/5622497 to your computer and use it in GitHub Desktop.
Save mzsanford/5622497 to your computer and use it in GitHub Desktop.
Sintra memory usage - Part 1
∴ 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 70252864198560
-- Starting GC
---- Waiting in memory (1848): #<MyApp:0x007fca08831ad8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>>
---- Waiting in memory (3576): #<MyApp:0x007fca0a0710a8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
-- Setting up array id 70252860706040
-- Starting GC
---- Waiting in memory (1848): #<MyApp:0x007fca08831ad8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>>
---- Waiting in memory (3576): #<MyApp:0x007fca099c92f0 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
---- Waiting in memory (3576): #<MyApp:0x007fca0a0710a8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
-- Setting up array id 70252868641540
-- Starting GC
---- Waiting in memory (1848): #<MyApp:0x007fca08831ad8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>>
---- Waiting in memory (3576): #<MyApp:0x007fca099c92f0 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
---- Waiting in memory (3576): #<MyApp:0x007fca0a0710a8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
---- Waiting in memory (3576): #<MyApp:0x007fca0a8f55a8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin
^C>> Stopping ...
== Sinatra has ended his set (crowd applauds)
****** GC Array(70252868641540) Starting
****** GC Array(70252860706040) Starting
****** GC Array(70252864198560) Starting
require 'sinatra/base'
require 'objspace'
# ruby-head only, Sorry.
class MyApp < Sinatra::Base
def initialize(*args)
puts "---- Init myApp"
super(*args)
end
get '/' do
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!
@rkh
Copy link

rkh commented May 21, 2013

The proc in the finalizer has a reference to the app instance, thus it's never released.

@rkh
Copy link

rkh commented May 21, 2013

@mzsanford
Copy link
Author

Ah, tricky. I'll try and use the class method approach on my live app and see where it leads me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment