<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arsalan Nawaz on Medium]]></title>
        <description><![CDATA[Stories by Arsalan Nawaz on Medium]]></description>
        <link>https://medium.com/@arsalan.nawaz1997?source=rss-fc2f112eb6c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*unNt4h4avl2vhrWb7i7zcw.png</url>
            <title>Stories by Arsalan Nawaz on Medium</title>
            <link>https://medium.com/@arsalan.nawaz1997?source=rss-fc2f112eb6c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 07:16:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arsalan.nawaz1997/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improving Memory Management in Rails. ActiveRecord::Base.uncached()]]></title>
            <link>https://medium.com/@arsalan.nawaz1997/improving-memory-management-in-rails-activerecord-base-uncached-bce3ece66075?source=rss-fc2f112eb6c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/bce3ece66075</guid>
            <category><![CDATA[activerecord]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[cache-control]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Arsalan Nawaz]]></dc:creator>
            <pubDate>Fri, 26 May 2023 10:05:00 GMT</pubDate>
            <atom:updated>2023-05-26T10:05:00.235Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eJY8Jo6mU9DQWL3VjzI-FA.png" /></figure><p>Have you ever faced errors for huge amounts of memory being used on your website. One of the most common reasons for that is memory overload.</p><h3>What is caching in Rails?</h3><p>You can find the basic definition and the the different ways in which rails caches your data for efficiency through the rails <a href="https://guides.rubyonrails.org/caching_with_rails.html">documentations on caching</a>.</p><p>In this article, we are going to talk about how you can use the rails <a href="https://apidock.com/rails/v5.2.3/ActiveRecord/QueryCache/ClassMethods/uncached">uncached()</a> method to prevent your app from retaining retrieved data for more than the required time because that keeps your memory occupied unnecessarily.</p><h3>What is happening actually</h3><p>When you load any data into your code, like assigning data retrieved from a model to a variable, for example <em>variable = Model.where(role: admin) </em>inside a method, this <em>variable</em> is going to persist in memory throughout the method that this variable resides in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*78rZeDQkqzpKRm16kPXOzQ.png" /></figure><p>as you can see in the picture above, for our functionality, we only need the value of <em>variable </em>from lines 4 through 6. but, right now, the value of <em>variable</em> still persists on line 8 and till end of <em>Method</em> even though we don’t need it anymore.</p><p>Now, consider that the records loaded in <em>variable</em> were millions so we are committing a considerable chunk of memory unnecessarily.</p><h3>ActiveRecord::Base.uncached()</h3><p>Now, adding the code above to an <em>uncached()</em> block</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qD93TblGzJGGCUGZQZYbEw.png" /></figure><p>Now, within the <em>ActiveRecord::Base.uncached</em> block, data within variable will persists and as soon as that block ends, the <em>variable </em>will be discarded from the memory.</p><p>This can also be achieved by writing <em>Model.uncached do</em> instead of <em>ActiveRecord::Base.uncached do.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bce3ece66075" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>