Pixelz Pixelz - 6 months ago 13
Ruby Question

Premature ruby exit on RAM intensive program

I'm trying to make my Ruby file print every possible IP there can be.

However, it exits prematurely with no error.

require 'ipaddress' #Is Installed

RUBY_GC_HEAP_GROWTH_FACTOR = 0.1
#Changes How Much RAM Is Taken Every Request (Tested And Removed, Not The Problem)

#Exits After Here

ip = IPAddress "0.0.0.0/24" #First Range
ip.to('255.255.255.255') #End Range

#Exits Before This Point

out_file = File.new("out.txt", "w") #Create & OpenFile
out_file.puts("#{ip}") #Output Variable To File
out_file.close #Save And Exit
sleep(8)


RAM is not a problem, due to having 8Gb worth, and the code exiting before hitting my RAM limit.

Whats causing the program to shutdown?

Answer

I think you're on the right track but you should use the gem in a slightly different way:

ip = IPAddress "0.0.0.0/0"

File.open("out.txt", "w") do |file|
  ip.each do |ip_address|
    file.puts ip_address
  end
end

sleep(8)

This used a finite (and reasonable) amount of RAM on my laptop when I tested it. However, I did not write out to a file, just to the console. Hopefully you are prepared to deal with the size of file this will produce.