Skip to content

Instantly share code, notes, and snippets.

@philou
Last active December 12, 2015 02:08
Show Gist options
  • Save philou/4696311 to your computer and use it in GitHub Desktop.
Save philou/4696311 to your computer and use it in GitHub Desktop.
The poor man's memory profiling
it "should use constant memory" do
warm_up_measure = memory_usage_for_items(1)
small_inputs_memory = memory_usage_for_items(1)
large_inputs_memory = memory_usage_for_items(200)
large_inputs_memory.should be <= small_inputs_memory * 1.05
end
def memory_usage_for_items(item_count)
data = setup(item_count)
memory_peak_of do
process(data)
end
end
def memory_peak_of
peak_usage = 0
finished = false
initial_usage = current_living_objects
profiler = Thread.new do
while not finished
peak_usage = [peak_usage, current_living_objects].max
sleep(0.01)
end
end
yield
finished = true
profiler.join
peak_usage - initial_usage
end
def current_living_objects
GC.start
object_counts = ObjectSpace.count_objects
object_counts[:TOTAL] - object_counts[:FREE]
end
def setup(items_count)
...
end
def process(data)
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment