# Saving to the DB uses a lot of RAM?

**URL:** <https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566>\
**Category:** Rails\
**Created:** [January 30, 2010, 2:12am UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566 "2010-01-30T02:12:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [January 30, 2010, 2:12am UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/1 "2010-01-30T02:12:19Z")

</div>

Hi guys,

I must be missing something obvious…

array\_of\_stuff = [contains a lot of fairly small AR objects]

# RAM at 30MB

array\_of\_stuff.each do |foo|  
foo.save  
end  
array\_of\_stuff = nil

# RAM at 60+MB

The iteration itself takes 30+MB of RAM (I measure with "memory\_usage  
from #{pcaller} at l.#{pline}: "+`ps -o rss= -p #{$$}`.to\_i.to\_s).

I don’t get it. Why is this using so much memory?

Thanks a lot,  
Pierre

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [January 30, 2010, 2:37pm UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/2 "2010-01-30T14:37:15Z")

</div>

PierreW wrote:

> Hi guys,
> 
> I must be missing something obvious…
> 
> array\_of\_stuff = [contains a lot of fairly small AR objects]
> 
> # RAM at 30MB
> 
> array\_of\_stuff.each do |foo|  
> foo.save  
> end  
> array\_of\_stuff = nil

This is terrible! You should never have a DB query inside a loop.  
Instead, generate one query to insert all the records. The  
ar-extensions plugin can help with that.

> # RAM at 60+MB
> 
> The iteration itself takes 30+MB of RAM (I measure with "memory\_usage  
> from #{pcaller} at l.#{pline}: "+`ps -o rss= -p #{$$}`.to\_i.to\_s).
> 
> I don’t get it. Why is this using so much memory?
> 
> Thanks a lot,  
> Pierre

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [February 1, 2010, 4:43am UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/3 "2010-02-01T04:43:05Z")

</div>

I am sorry but I don’t have an answer about the RAM question. However  
I would like to answer Marnen’s comment. While I agree that letting  
the DB do the work for mass record processing should be the best and  
most efficient way to go by reading the OP one cannot assume that is  
the way things are in this case. The array used by the OP could very  
well contain tons of different types of objects, used for very  
different purposes and not necessarily related to each other. Pierre  
never gave any indication it was one way or the other.

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [February 1, 2010, 1:53pm UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/4 "2010-02-01T13:53:43Z")

</div>

pepe wrote:

> I am sorry but I don’t have an answer about the RAM question. However  
> I would like to answer Marnenï¿½s comment. While I agree that letting  
> the DB do the work for mass record processing should be the best and  
> most efficient way to go by reading the OP one cannot assume that is  
> the way things are in this case.

No, but it’s likely.

> The array used by the OP could very  
> well contain tons of different types of objects, used for very  
> different purposes and not necessarily related to each other.

Then for the purpose of saving, they should be separated out by type.  
Queries don’t go in loops. Period.

> Pierre  
> never gave any indication it was one way or the other.

True. So why “correct” me with an unlikely exception to the general  
principle?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [February 1, 2010, 11:28pm UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/5 "2010-02-01T23:28:18Z")

</div>

On Feb 1, 7:53 am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> pepe wrote:
> 
> > I am sorry but I don’t have an answer about the RAM question. However  
> > I would like to answer Marnen s comment. While I agree that letting  
> > the DB do the work for mass record processing should be the best and  
> > most efficient way to go by reading the OP one cannot assume that is  
> > the way things are in this case.
> 
> No, but it’s likely.

Anything is likely.

> > The array used by the OP could very  
> > well contain tons of different types of objects, used for very  
> > different purposes and not necessarily related to each other.
> 
> Then for the purpose of saving, they should be separated out by type.  
> Queries don’t go in loops. Period.

Says who? The point I was making is that it would depend on the  
situation and the solution the OP is trying to give to his particular  
problem.

> > Pierre  
> > never gave any indication it was one way or the other.
> 
> True. So why “correct” me with an unlikely exception to the general  
> principle?

I wasn’t trying to “correct” you. I was trying to offer a different  
point of view and have an open mind.

Cheers.

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [February 2, 2010, 12:03am UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/6 "2010-02-02T00:03:51Z")

</div>

pepe wrote:

> On Feb 1, 7:53ï¿½am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > pepe wrote:
> > 
> > > I am sorry but I don’t have an answer about the RAM question. However  
> > > I would like to answer Marnen s comment. While I agree that letting  
> > > the DB do the work for mass record processing should be the best and  
> > > most efficient way to go by reading the OP one cannot assume that is  
> > > the way things are in this case.
> > 
> > No, but it’s likely.
> 
> Anything is likely.

Anything is _possible_…but it’s…er…unusual to be saving an array  
of unrelated objects.

> > > The array used by the OP could very  
> > > well contain tons of different types of objects, used for very  
> > > different purposes and not necessarily related to each other.
> > 
> > Then for the purpose of saving, they should be separated out by type.  
> > Queries don’t go in loops. ï¿½Period.
> 
> Says who?

Anyone who understands how to use databases efficiently end effectively.

> The point I was making is that it would depend on the  
> situation and the solution the OP is trying to give to his particular  
> problem.
> 
> > > Pierre  
> > > never gave any indication it was one way or the other.
> > 
> > True. ï¿½So why “correct” me with an unlikely exception to the general  
> > principle?
> 
> I wasn’t trying to “correct” you.

Yes, I realized that after I posted.

> I was trying to offer a different  
> point of view and have an open mind.

OK, but in this case I don’t think it was relevant.

> Cheers.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [February 1, 2010, 5:14am UTC](https://www.ruby-forum.com/t/saving-to-the-db-uses-a-lot-of-ram/182566/7 "2010-02-01T05:14:07Z")

</div>

On Fri, Jan 29, 2010 at 8:10 PM, PierreW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> Pierre

Pierre, you might be interested in the following thread within this  
mailing  
list because it covered a similar topic:

“activerecord 2.3.5’s create & find slower than activerecord 2.1.2”

Good luck,

-Conrad
