Skip to content

Instantly share code, notes, and snippets.

@ganmacs
Last active July 29, 2019 03:29
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 ganmacs/f099e9f95c1c0191de65616a1bbf463f to your computer and use it in GitHub Desktop.
Save ganmacs/f099e9f95c1c0191de65616a1bbf463f to your computer and use it in GitHub Desktop.
class FIFO
def initialize
@buffer = ''
@eol = "\n"
end
def <<(chunk)
@buffer << chunk
end
def next_line
idx = @buffer.index(@eol)
@buffer.slice!(0, idx + 1) unless idx.nil?
end
def next_line2
idx = @buffer.index(@eol)
unless idx.nil?
@buffer.freeze
rbuf = @buffer.slice(0, idx + 1)
@buffer = @buffer.slice(idx + 1, @buffer.size)
rbuf
end
end
end
log = "this is a log\n".freeze
f1 = FIFO.new
N = 5
N.times do
f1 << log
end
require 'memory_profiler'
size = 0
if ENV["SLICE_BANG"]
report = MemoryProfiler.report do
N.times do
size += f1.next_line.size
end
end
else
report = MemoryProfiler.report do
N.times do
size += f1.next_line2.size
end
end
end
report.pretty_print
p size
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "benchmark-memory"

slice! version

$ SLICE_BANG=be ruby app2.rb                                                                                                  
Total allocated: 716 bytes (13 objects)
Total retained:  0 bytes (0 objects)

allocated memory by gem
-----------------------------------
       716  other

allocated memory by file
-----------------------------------
       716  app2.rb

allocated memory by location
-----------------------------------
       716  app2.rb:13

allocated memory by class
-----------------------------------
       716  String

allocated objects by gem
-----------------------------------
        13  other

allocated objects by file
-----------------------------------
        13  app2.rb

allocated objects by location
-----------------------------------
        13  app2.rb:13

allocated objects by class
-----------------------------------
        13  String

retained memory by gem
-----------------------------------
NO DATA

retained memory by file
-----------------------------------
NO DATA

retained memory by location
-----------------------------------
NO DATA

retained memory by class
-----------------------------------
NO DATA

retained objects by gem
-----------------------------------
NO DATA

retained objects by file
-----------------------------------
NO DATA

retained objects by location
-----------------------------------
NO DATA

retained objects by class
-----------------------------------
NO DATA


Allocated String Report
-----------------------------------
         5  ""
         5  app2.rb:13

         5  "this is a log\n"
         5  app2.rb:13

         1  "this is a log\nthis is a log\nthis is a log\n"
         1  app2.rb:13

         1  "this is a log\nthis is a log\nthis is a log\nthis is a log\n"
         1  app2.rb:13

         1  "this is a log\nthis is a log\nthis is a log\nthis is a log\nthis is a log\n"
         1  app2.rb:13

70

slice + freeze version

$ ruby app2.rb                                                                                                                
Total allocated: 400 bytes (10 objects)
Total retained:  40 bytes (1 objects)

allocated memory by gem
-----------------------------------
       400  other

allocated memory by file
-----------------------------------
       400  app2.rb

allocated memory by location
-----------------------------------
       200  app2.rb:21
       200  app2.rb:22

allocated memory by class
-----------------------------------
       400  String

allocated objects by gem
-----------------------------------
        10  other

allocated objects by file
-----------------------------------
        10  app2.rb

allocated objects by location
-----------------------------------
         5  app2.rb:21
         5  app2.rb:22

allocated objects by class
-----------------------------------
        10  String

retained memory by gem
-----------------------------------
        40  other

retained memory by file
-----------------------------------
        40  app2.rb

retained memory by location
-----------------------------------
        40  app2.rb:22

retained memory by class
-----------------------------------
        40  String

retained objects by gem
-----------------------------------
         1  other

retained objects by file
-----------------------------------
         1  app2.rb

retained objects by location
-----------------------------------
         1  app2.rb:22

retained objects by class
-----------------------------------
         1  String


Allocated String Report
-----------------------------------
         6  "this is a log\n"
         5  app2.rb:21
         1  app2.rb:22

         1  ""
         1  app2.rb:22

         1  "this is a log\nthis is a log\n"
         1  app2.rb:22

         1  "this is a log\nthis is a log\nthis is a log\n"
         1  app2.rb:22

         1  "this is a log\nthis is a log\nthis is a log\nthis is a log\n"
         1  app2.rb:22


Retained String Report
-----------------------------------
         1  ""
         1  app2.rb:22

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