简体   繁体   中英

How to ensure that a big file (blob) is released from memory in ruby?

I have an app which receives an incoming file (attachment in the form of blob_data), and just passes it on, to another external service/API (while storing the attachment metadata in my app). I don't need to store the blob_data in my app.

class Attachment < ActiveRecord::Base
  attr_accessor :blob_data
end

When I call @attachment.save I presume the blob_data won't be persisted to the DB, because it is an attr_accessor field. But, is there a way to ensure that this blob_data is released from memory immediately? I don't want the blob_data lingering around until the GC catches it at a later time, as it may take up quite a bit of memory, especially if the server receives several requests in the same time period.

Would it be as simple as using @attachment.blob_data = nil ? Will this have any effect on the immediacy of the GC catching it? Is there a better way?

根据这篇文章,您唯一可以做的就是删除对blob_data所有引用并等待垃圾收集开始。正如帖子和blob_data的评论都提到的,解决问题的更有效方法是流式传输文件无需将其存储在内存中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM