Answer the question
In order to leave comments, you need to log in
How to free memory while running a Ruby script?
Good afternoon, in the course of studying ruby, I encountered a problem that during the operation of my "test.rb" file, a lot of memory is being eaten, gluttony is growing, 30MB after 5 minutes is already 40 - 50MB.
I tried to cut all "print & puts" well, I thought because of the print to the console and devours = did not help.
Tried to clear all variables before starting a new thread = did not help
Tried to limit the number of threads = did not help (waits until 10 threads finish, starts when finished)
Who can tell what could be the problem? Maybe before starting a new thread in Ruby there is a magic function "freeMemory()" that I don't know about :)
class TestParse
attr_accessor :clientM
attr_accessor :queries
attr_accessor :threads
def initialize()
@threads = []
@clientM = client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => '', :database => 'db')
end
def generateUrl()
groupI = 0
@queries = []
while groupI <= 20 do
prepareQuery()
groupI = groupI+1;
end
end
def prepareQuery
@thread << .......HTTP QUERY.......
hash = JSON.parse(resp.body)
if (resp.code == "200")
@queries << "INSERT INTO `posts` (`text`,`type`) VALUES ('#{@clientM.escape(a['text'])}',1);"
end
end
end
s = TestParse.new()
isgl = 0
while isgl < 20
s.generateUrl
s.threads.each { |thr| thr.join }
print "Query: "+s.queries.length.to_s+"\n"
iQuery = 0
s.queries.each { |q|
s.clientM.query(q)
}
print "==================\n"
s.queries = []
isgl = isgl+1
end
Answer the question
In order to leave comments, you need to log in
Ruby itself is voracious in terms of memory. And if you use threads, then even more so.
So calm down.
gluttony is growing, 30MB after 5 minutes is already 40 - 50MB.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question