A
A
Andrey Sokolov2015-08-13 09:19:23
ruby
Andrey Sokolov, 2015-08-13 09:19:23

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

2 answer(s)
I
Ivan Kryak, 2015-08-13
@sck_v

Ruby itself is voracious in terms of memory. And if you use threads, then even more so.
So calm down.

V
Viktor Vsk, 2015-08-13
@viktorvsk

gluttony is growing, 30MB after 5 minutes is already 40 - 50MB.

And after 10 and 15 minutes?
50 megabytes for languages ​​like ruby, python, etc. - this is normal
OS memory such languages ​​do not return. They mark it free when the garbage collector gets to it, and such memory can be occupied by new objects.
In such cases, it is customary to kill gluttonous processes. For example, do the job through the Unix fork() and kill it when it's done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question