SaravanaKumAr SaravanaKumAr - 5 months ago 22
Ruby Question

Ruby code memory leak in loop

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.

Dbz Dbz
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
Comments