# Manual Garbage Collection

**URL:** <https://www.ruby-forum.com/t/manual-garbage-collection/106746>\
**Category:** Ruby\
**Created:** [June 13, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/manual-garbage-collection/106746 "2007-06-13T19:40:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/manual-garbage-collection/106746/1 "2007-06-13T19:40:47Z")

</div>

Hi. I’m having a problem with a Ruby-Gnome2 application I’ve been  
building. For some reason(which I haven’t identified but I suspect is  
ruby-gnome problem) my program randomly segfaults. I fixed this by  
disabling garbage collection(Maybe GC was cleaning up objects that were  
still in use?) but of course now my application runs out of memory  
relatively quickly. I know which parts of my non-gnome related code are  
memory hogs, and I was wondering if there is a way to manually garbage  
collect specific objects(and their dependencies hopefully)? Or,  
alternatively, is their a way to only run garbage collection within a  
certain scope?

I’d love a solution to this, as my current alternatives are either  
running out of memory or random segfaults.

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/manual-garbage-collection/106746/2 "2007-06-13T20:16:04Z")

</div>

On 6/13/07, Conan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi. I’m having a problem with a Ruby-Gnome2 application I’ve been  
> building. For some reason(which I haven’t identified but I suspect is  
> ruby-gnome problem) my program randomly segfaults. I fixed this by  
> disabling garbage collection(Maybe GC was cleaning up objects that were  
> still in use?)

Not very likely, the GC if anything tends not to free objects which  
are still referenced rather than the other way around. Segfaults are  
almost always caused by flawed extension code in C/C++ I think that  
ruby-gnome2 might have a bit of that. .

I’ve not used or looked at Ruby-Gnome2 but I’d look to see if it was  
using finalizers. It could be that the bug is being exposed by their  
code which runs in a finalizer. Turning off GC would prevent this  
code from running.

> but of course now my application runs out of memory  
> relatively quickly. I know which parts of my non-gnome related code are  
> memory hogs, and I was wondering if there is a way to manually garbage  
> collect specific objects(and their dependencies hopefully)? Or,  
> alternatively, is their a way to only run garbage collection within a  
> certain scope?

I’m afraid not.

> I’d love a solution to this, as my current alternatives are either  
> running out of memory or random segfaults.

Or tracking down what’s actually segfaulting. Perhaps there are some  
ideas in this thread:  
[http://www.ruby-forum.com/topic/110106](http://www.ruby-forum.com/topic/110106)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/manual-garbage-collection/106746/3 "2007-06-13T20:16:04Z")

</div>

You can GC.start to run it manually. If you can I’d recommend trying to  
figure out what object is getting improperly GC’d and let the  
Ruby-Gnome2  
team know about this. I’ve seen this kind of thing happen when Ruby  
tries to  
send it’s own objects back into the underlying library. For me it was  
Ruby  
subclasses of C++ classes and dealing with setting listener objects.

Jason

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 14, 2007, 12:07am UTC](https://www.ruby-forum.com/t/manual-garbage-collection/106746/4 "2007-06-14T00:07:03Z")

</div>

Conan R. wrote:

> Ok, I’ll see if I can find(and hey, maybe disable) ruby-gnome’s  
> finalizers that may be causing problems. Thanks for your help, at least  
> now I’ve got an idea of where to look.

If anyone is interested in knowing more, I’ve continued this thread in  
[Garbage Collection causes segfaults - Ruby-Gnome 2 - Ruby-Forum](http://www.ruby-forum.com/topic/111736) with code examples.

---

<div class="post-metadata">

**Author:** ![conan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/13edae/32.png) [@conan](https://www.ruby-forum.com/u/conan)\
**Post date:** [June 13, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/manual-garbage-collection/106746/5 "2007-06-13T23:22:12Z")

</div>

Ok, I’ll see if I can find(and hey, maybe disable) ruby-gnome’s  
finalizers that may be causing problems. Thanks for your help, at least  
now I’ve got an idea of where to look.
