Magne
Magne

Reputation: 17243

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?

Upvotes: 2

Views: 114

Answers (1)

user1351104
user1351104

Reputation:

As per this post, the only thing you can do is remove all references to blob_data and wait for garbage collection to kick in. As the post and @max's comment both mention, a more efficient way of solving your problem would be to stream the file without storing it in memory.

Upvotes: 2

Related Questions