# Are not used Objects cleaned from memory?

**URL:** <https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915>\
**Category:** Ruby\
**Created:** [July 13, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915 "2008-07-13T13:45:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 13, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915/1 "2008-07-13T13:45:43Z")

</div>

Hi, a simple question:  
If I have an Array containing custom complex objects and I use  
Array.delete or  
any other function that deletes a element of the Array, will be the  
deleted  
Object remain in RAM memory? or will it automatically deleted?  
Note that the object also contains complex objects as attributes.

Thanks.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 13, 2008, 2:58pm UTC](https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915/2 "2008-07-13T14:58:52Z")

</div>

IÃ±aki Baz C. wrote:

> If I have an Array containing custom complex objects and I use Array.delete  
> or any other function that deletes a element of the Array, will be the  
> deleted Object remain in RAM memory? or will it automatically deleted?

Assuming the object is not referenced in any other way it will be  
garbage  
collected.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 14, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915/3 "2008-07-14T16:33:00Z")

</div>

Sebastian H. wrote:

> Assuming the object is not referenced in any other way it will be  
> garbage  
> collected.

Eventually.

AFAIK there is no way to force the garbage collector to operate; this is  
in common with most other languages that use garbage collection.  
GC.start or GC.enable do not _force_ garbage collection to occur.

But I’m by no means an expert, so someone will probably be along in a  
minute to tell you about a gem that does just that… or it’s already in  
v1.9… 🙂

Dave

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 15, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915/4 "2008-07-15T16:21:45Z")

</div>

On 14/07/2008, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> GC.start or GC.enable do not _force_ garbage collection to occur.
> 
> But I’m by no means an expert, so someone will probably be along in a  
> minute to tell you about a gem that does just that… or it’s already in  
> v1.9… 🙂

There might be some non-obvious references to objects stored in blocks  
or elsewhere. When you write a piece of code (such as a block) and  
save it somewhere the variables that were visible while writing the  
block have to be kept around.

Also from what I have heard the GC works by scanning the memory  
(stack, whatever) for things that _look_ like object pointers. So if  
you have a number (or substring, or …) that points to an object when  
interpreted as a pointer that object would not be deleted even though  
it is really not referenced …

When I created lots of small strings and stuffed them into numerous  
hashes and arrays the objects did not go away as I would expect. I  
saved a gigabyte or two of ram by using JRuby then - it required about  
half the memory in this case 😉

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [July 16, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/are-not-used-objects-cleaned-from-memory/142915/5 "2008-07-16T18:50:34Z")

</div>

> AFAIK there is no way to force the garbage collector to operate;

Hmm GC.start ?

Or maybe wait until you have 8M used, then it should kick in … 🙂
