I have an issue where Net/HTTP library is causing very high memory usage when requesting for medium to large sized content.
Here is the script to reproduce the issue:
gem 'excon'
require 'uri'
require 'net/http'
require 'excon'
uri = URI('https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_1920_18MG.mp4')
def print_memory
mem = `ps -o rss -p #{Process::pid}`.chomp.split("\n").last.strip.to_i
puts "Memory: #{mem/1024} MB"
end
25.times do
case ARGV[0]
when 'EXCON' then Excon.get(uri.to_s)
when 'NET' then Net::HTTP.get_response(uri)
end
print_memory
end
The script downloads 18MB size video file 25 times in a row and prints the process memory. After 25 iterations, Net/HTTP ends up almost 300MB of ram, while Excon only 60MB. Here is the complete result of RAM size the process used after each iteration:
Subject changed from Net/HTTP consuming multiple times more memories compared to other libraries to Net/HTTP consuming multiple times more memory compared to other libraries
Hey Yusuke, thanks for checking this out. I have tried this out on my friends machines and one of them has exactly the same results as I, but other has the results that are matching yours. All three of us are using MacBooks (although different models). However, running it in Docker (FROM ruby:2.6.5) it gave us consistent results where Excon is actually consuming more memory. Furthermore, I have also tried running this on Heroku dyno and here are the results: