Skip to content

Instantly share code, notes, and snippets.

@xTRiM
Last active March 6, 2021 06:58
Show Gist options
  • Save xTRiM/afdd9ea25519a479672153637637f0f7 to your computer and use it in GitHub Desktop.
Save xTRiM/afdd9ea25519a479672153637637f0f7 to your computer and use it in GitHub Desktop.
Memory leak
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "ruby-xz"
gem "get_process_mem"
end
require "xz"
require "objspace"
def self.record_allocation
GC.start
GC.start
GC.compact
mem = GetProcessMem.new
puts "Before - Process Memory: #{init_mem=mem.mb.round(3)} mb"
puts "Before - Objects count: #{init_obj_cnt=ObjectSpace.each_object.count}"
puts "Before - Symbols count: #{init_sym_cnt=Symbol.all_symbols.size}"
yield
GC.start
GC.start
GC.start
puts "After - Process Memory: #{mem.mb} mb, changed by #{(mem.mb-init_mem).round(3)} mb"
puts "After - Objects count: #{ObjectSpace.each_object.count}, changed by #{init_obj_cnt-ObjectSpace.each_object.count}"
puts "After - Symbols count: #{Symbol.all_symbols.size}, changed by #{init_sym_cnt-Symbol.all_symbols.size}"
end
iterations = (ARGV[0].to_i > 0) ? ARGV[0].to_i : 10000
def compress_only(iterations)
data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
io = StringIO.new(data)
record_allocation do
iterations.times do
io.rewind
StringIO.new(XZ.compress_stream(io))
end
end
end
def decompress_only(iterations)
data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
io = StringIO.new(data)
compressed = StringIO.new(XZ.compress_stream(io))
record_allocation do
iterations.times do
XZ.decompress_stream(compressed)
end
end
end
def compress_decompress(iterations)
data = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
io = StringIO.new(data)
record_allocation do
iterations.times do
io.rewind
compressed = StringIO.new(XZ.compress_stream(io))
XZ.decompress_stream(compressed)
end
end
end
puts "[Compress run with #{iterations} iterations]"
compress_only(iterations)
puts "[Decompress run with #{iterations} iterations]"
decompress_only(iterations)
puts "[Compress/decompress run with #{iterations} iterations]"
compress_decompress(iterations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment