Ruby code memory leak in loop

1.8k views Asked by At

Below code is having memory leak. It's running under ruby 2.1.1. I am not able to find the actual leak.

q = Queue.new("test")
while true do
  m = q.dequeue
  body = JSON.parse(m.body)
  user_id = body["Records"][0]
  user = V2::User.find(user_id)
  post = V2::Post.find(post_id)  
end

After few hours of run I added GC.start but its not solving the problem

q = Queue.new("test")
while true do
  m = q.dequeue
  body = JSON.parse(m.body)
  user_id = body["Records"][0]
  user = V2::User.find(user_id)
  post = V2::Post.find(post_id) 
  GC.start 
end

I don't know how to find the actual memory leak.

2

There are 2 answers

5
Dbz On BEST ANSWER

Try removing the lines from the bottom up, and seeing if the memory leak persists. It's possible that the Memory leak is coming from the find method, or possibly the JSON.parse (extremely unlikely), or the custom Queue data structure. If the memory leak is still there after removing all of the lines, it is likely coming from the worker itself and/or the program running the workers.

q = Queue.new("test")
while true do
  m = q.dequeue # Finally remove this and stub the while true with a sleep or something
  body = JSON.parse(m.body) # Then remove these two lines
  user_id = body["Records"][0]
  user = V2::User.find(user_id) # Remove the bottom two lines first
  post = V2::Post.find(post_id)
end
8
Aleksei Matiushkin On

I bet the problem is with introduced local variables (sic!). Get rid of user_id and post_id and it’ll likely stop leaking:

# user_id = body["Records"][0]
# user = V2::User.find(user_id)
user = V2::User.find(body["Records"][0]) # sic!

The reason is how Ruby stores objects in RValues.