[LRUG] Request for assistance with Rails performance oddity

Roland Swingler roland.swingler at gmail.com
Sat Oct 30 05:50:35 PDT 2010


> Is there a way of telling ActiveRecord to discard any cached database
records it currently has in memory?

I don't think you can quite do this - although you can rely on the
ruby garbage to collector to remove any objects that you no longer
need, so you could just re-find your root record if that is all you're
really interested in.

i.e.

@category = EventCategory.find(whatever)
# your code to remove specific events
@category.events.each { .... }
@category = EventCategory.find(whatever)

The original EventCategory will no longer be referenced anywhere
(hopefully), and neither will any of the events, so the memory they
use will be reclaimed.

Will this do what you mean?

You're probably better off implementing an efficient way of removing
the events within a certain range though, rather than trying to work
around the problem like this.

HTH
Roland

On Sat, Oct 30, 2010 at 10:07 AM, John Winters <john at sinodun.org.uk> wrote:
> On 30/10/10 07:17, Levent Ali wrote:
>> Could activerecord be trying to load existing records into memory?
>
> Funnily enough - yes.
>
> I've got a bit further with figuring this out, and it's caused by an
> after-effect of an earlier bit of code.
>
> Before loading events for a range of dates the code purges any existing
> events in that range.  It does this by the extraordinarily inefficient
> method of loading all events and destroying all those in the specified
> range.  (In my defence, there is a note just above that line of code
> saying it needs to be converted to a single SQL call.)
>
> It seems that the remaining events (still 26858 of them) then hang
> around in memory and ActiveRecord takes a quick glance at each of them
> before creating each new event record.  Lots of quick glances add up to
> a load of CPU time.
>
> By purging old events using a separate run of the script and then
> loading new events with a fresh run I can get back to the original fast
> speed.
>
> Is there a way of telling ActiveRecord to discard any cached database
> records it currently has in memory?
>
> TIA,
> John
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>



More information about the Chat mailing list