Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do objects sent with send_data seem to be kept in memory?

I'm building a Rails app (5.0.4, Ruby 2.3.3) that creates a PDF file with Prawn, and sends it to the user, using ActionController's send_data, like this:

 # data is a Prawn document    
 send_data data, type: 'application/pdf', disposition: 'inline', filename: 'document.pdf'

I've noticed on Heroku that the app seems to be using more and more memory with each document that is created and sent. I confirmed this on my local machine with some crude tests. Now, I placed GC.start after the send_data call and the problem seems to be resolved. Memory always goes back to a stable level.

My questions are, is send_data doing something to keep the object around for a long time? And isn't the garbage collector supposed to run "on its own" from time to time? Heroku shows a steady memory increase. Thank you.

like image 504
flavio Avatar asked Jan 02 '26 02:01

flavio


1 Answers

The GC will run only when the system is under some memory pressure, specifically when the heap_free_slots are exhausted. You can see how many are available with GC.stat. Since you don't typically pay for used memory until you run out of it, there's little point in trying to free up space prematurely. Then the most space can be reclaimed in the fewest invocations of the garbage collector.

like image 160
Segfault Avatar answered Jan 03 '26 15:01

Segfault



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!