ruby-on-railsrubymemory-leaksdelayed-jobmemory-profiling

Want to determine whether we have memory leak in ROR


In our project we are your sendgrid to send emails and delayed job for queue purpose, I upgraded ruby to 2.7 ,rails to 6.0.3.6, delayed_job_active_record to 4.1.6 . After upgrading we are noticing out of memory in aws container. Wanted to know whether it is because of memory leak. If it is because of memory leak what profiler I need to use to determine memory leak?


Solution

  • Because of garbage collection, ruby will never have the kind of memory leak that occurs in C/C++ where the program was supposed to free the memory it no longer needs but did not.

    What can happen is runaway memory because you are holding on to references which you don't need. Typically this happens when you keep things in class instance collections but don't cull the list as things are unneeded or old.

    Another thing which can happen is an interaction between ruby memory management and the OS memory allocator. There is a very good article, What causes Ruby memory bloat by Hongli Lai on this. This might be something you can do very little about since it is not a memory "leak" in your code.

    A feature was added in ruby 2.7 which addresses the issue in Hongli Lai's article. The method is GC.compact which is not called automatically but will defragment the ruby heap.