# DRb cleaning up client objects created by clients

**URL:** <https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011>\
**Category:** Ruby\
**Created:** [January 24, 2008, 6:12am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011 "2008-01-24T06:12:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 6:12am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/1 "2008-01-24T06:12:56Z")

</div>

Hopefully the below makes sense…

My DRb server’s front object has a method called ‘create\_foo’ which  
creates & returns a new instance of class Foo. The Foo class must  
keep track of the number of active Foo objects and enforces a limit.  
Well behaved clients call my\_foo\_obj.free before releasing their  
reference to my\_foo\_obj which updates the Foo class tracker. Of  
course, not all clients are well behaved (they crash, user CTRL-C’s  
them, server they’re on get rebooted, etc). The result is that stale  
objects stay around indefinitely and are never marked as released in  
the Foo class. Foo class leaks objects and ends up stoping creating  
new ones.

I’ve tried creating a Foo.finalizer method which does the free(), but  
that doesn’t work, even when I force garbage collection. Both the  
front object and Foo class are DRbUndumped.

Anyways, it would be great if there was a way for the DRb server to  
detect a client has disconnected and react accordingly, but I can’t  
find any way to hook into the DRb class to do that. Suggestions?

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 6:49am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/2 "2008-01-24T06:49:45Z")

</div>

Aaron T. wrote:

> the Foo class. Foo class leaks objects and ends up stoping creating  
> new ones.
> 
> I’ve tried creating a Foo.finalizer method which does the free(), but  
> that doesn’t work, even when I force garbage collection. Both the  
> front object and Foo class are DRbUndumped.
> 
> Anyways, it would be great if there was a way for the DRb server to  
> detect a client has disconnected and react accordingly, but I can’t  
> find any way to hook into the DRb class to do that. Suggestions?

It’s problem with both GC and DRb.

finilizer will never be run becouse it’s run just before object is  
collected - since DRb is not releasing reference it’s newer collected.

try using object proxy (delegator)

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 7:44am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/3 "2008-01-24T07:44:07Z")

</div>

On Jan 23, 2008 9:48 PM, Marcin R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It’s problem with both GC and DRb.
> 
> finilizer will never be run becouse it’s run just before object is  
> collected - since DRb is not releasing reference it’s newer collected.

That tends to agree with my research

> try using object proxy (delegator)

Sorry not familiar with the module, concept or whatever you’re  
referring to. Link?

Thanks,  
Aaron

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 9:08am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/4 "2008-01-24T09:08:26Z")

</div>

Aaron T. wrote:

> Sorry not familiar with the module, concept or whatever you’re  
> referring to. Link?
> 
> Thanks,  
> Aaron

On server side you define empty object that ovverwrites method\_missing  
and every call is transported to coresponding “real” object.

It’s ugly hack, becouse proxy objects will never be collected unless  
issue is fixed in DRb, but that should help with memory usage (almost  
empty objects should not be really memory consuming)

and you can menage all the references server side.

If you are looking for better solution check packet and EventMachine,  
BackgroundRB recently switched to Packet becouse of DRb issues.

cheers

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 9:43am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/5 "2008-01-24T09:43:08Z")

</div>

> - use DRbUndumped to keep Foos on the server  
> he mentioned that he’s using that already

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 9:34am UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/6 "2008-01-24T09:34:54Z")

</div>

On Jan 23, 2008, at 10:12 PM, Aaron T. wrote:

> new ones.

- use DRbUndumped to keep Foos on the server

- store all active Foos in a list

- provide a callback to the client when constructing the Foos,  
possibly just a block passed to the method (since invocation of it  
would run on the client)

- whenever a new Foos is requested sweep the list invoking the  
callbacks/blocks. if the client has evaporated this will throw -  
nuke the Foo from the list

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![Aaron\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@Aaron\_T](https://www.ruby-forum.com/u/Aaron_T)\
**Post date:** [January 24, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/drb-cleaning-up-client-objects-created-by-clients/127011/7 "2008-01-24T17:50:21Z")

</div>

On Jan 24, 2008 12:33 AM, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> nuke the Foo from the list  
> Ah, yes, that should meet my needs. Thanks to everyone else who  
> responded with ideas.
