# How to free an unused object forcefully?

**URL:** <https://www.ruby-forum.com/t/how-to-free-an-unused-object-forcefully/121060>\
**Category:** Ruby\
**Created:** [November 23, 2007, 11:00am UTC](https://www.ruby-forum.com/t/how-to-free-an-unused-object-forcefully/121060 "2007-11-23T11:00:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 23, 2007, 11:00am UTC](https://www.ruby-forum.com/t/how-to-free-an-unused-object-forcefully/121060/1 "2007-11-23T11:00:57Z")

</div>

Hi All,

I have a script which serves as a cache, storing “large” (around  
60MB) objects. Once in a day I have to drop  
the old data and load fresh data in the cache. The cache itself is  
simply a hash of hashes, nothing special. I thought of a simple  
process: I wrote a loader method,

def loader  
tmp\_holder = get\_new\_data(somehow)  
$actual\_cache = tmp\_holder  
end

I expected tmp\_holder to be freed after the method finishes, since  
it’s not referred to anywhere else. What’s actually  
happening is that my script is using more and more memory as days  
pass, as if tmp\_holder was still present.  
How could I solve this situation?

Thank you in advance,  
Ochronus

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 2, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/how-to-free-an-unused-object-forcefully/121060/2 "2007-12-02T16:22:03Z")

</div>

On Nov 23, 2007 8:00 AM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $actual\_cache = tmp\_holder  
> end
> 
> I expected tmp\_holder to be freed after the method finishes, since  
> it’s not referred to anywhere else. What’s actually  
> happening is that my script is using more and more memory as days  
> pass, as if tmp\_holder was still present.  
> How could I solve this situation?

tmp\_holder is still being referenced, since you’re assigning it to  
that global variable.

The example below seems to be more GC-friendly to me, but since I’m  
not exactly an expert at this, I may be wrong. But it might be worth  
trying:

$actual\_cache.clear  
$actual\_cache.merge(get\_new\_data(somehow))
