Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > IRB + C Extension = Memory Loss

Reply
Thread Tools

IRB + C Extension = Memory Loss

 
 
Ryan Bates
Guest
Posts: n/a
 
      09-19-2008
I'm having difficult making a simple C extension. The struct's memory
seems to go missing after a while when using IRB. I'm no C expert, so
I'm probably doing something wrong as far as memory allocation is
concerned. Here's the problem and the code:

http://gist.github.com/11611

Notice the greeting goes away after calling "h.greet" a number of
times? This only seems to happen in IRB and not through a ruby script.
Any idea why? I'm assuming it has something to do with Ruby's garbage
collection, but I don't know how to solve it.

Thanks,

Ryan

 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      09-19-2008
Ryan Bates wrote:
> I'm having difficult making a simple C extension. The struct's memory
> seems to go missing after a while when using IRB. I'm no C expert, so
> I'm probably doing something wrong as far as memory allocation is
> concerned. Here's the problem and the code:
>
> http://gist.github.com/11611
>
> Notice the greeting goes away after calling "h.greet" a number of
> times? This only seems to happen in IRB and not through a ruby script.
> Any idea why? I'm assuming it has something to do with Ruby's garbage
> collection, but I don't know how to solve it.
>
> Thanks,
>
> Ryan


GC is collecting the greeting string because it thinks there are no
references to it. Your hello_mark function needs to identify the
greeting string object as "in use". From the Pickaxe:

"The mark routine will be called by the garbage collector during its
``mark'' phase. If your structure references other Ruby objects, then
your mark function needs to identify these objects using
rb_gc_mark(value)."
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ryan Bates
Guest
Posts: n/a
 
      09-19-2008
Thanks! That makes sense. Calling rb_gc_mark(*hello->greeting) in the
hello_mark method works.

On a related note. What if I need to re-assign greeting to a new
string at some point in the future after hello_mark has been called.
Should I call rb_gc_mark upon assignment (outside of hello_mark)?

Regards,

Ryan


On Sep 19, 10:00=A0am, Tim Hunter <(E-Mail Removed)> wrote:
> Ryan Bates wrote:
> > I'm having difficult making a simple C extension. The struct's memory
> > seems to go missing after a while when using IRB. I'm no C expert, so
> > I'm probably doing something wrong as far as memory allocation is
> > concerned. Here's the problem and the code:

>
> >http://gist.github.com/11611

>
> > Notice the greeting goes away after calling "h.greet" a number of
> > times? This only seems to happen in IRB and not through a ruby script.
> > Any idea why? I'm assuming it has something to do with Ruby's garbage
> > collection, but I don't know how to solve it.

>
> > Thanks,

>
> > Ryan

>
> GC is collecting the greeting string because it thinks there are no
> references to it. Your hello_mark function needs to identify the
> greeting string object as "in use". From the Pickaxe:
>
> "The mark routine will be called by the garbage collector during its
> ``mark'' phase. If your structure references other Ruby objects, then
> your mark function needs to identify these objects using
> rb_gc_mark(value)."
> --
> Posted viahttp://www.ruby-forum.com/.


 
Reply With Quote
 
Tim Hunter
Guest
Posts: n/a
 
      09-19-2008
Ryan Bates wrote:
> Thanks! That makes sense. Calling rb_gc_mark(*hello->greeting) in the
> hello_mark method works.
>
> On a related note. What if I need to re-assign greeting to a new
> string at some point in the future after hello_mark has been called.
> Should I call rb_gc_mark upon assignment (outside of hello_mark)?
>
> Regards,
>
> Ryan


Let's back up a bit. I assume you're storing the VALUE passed into
hello_init in hello->greeting:

static VALUE hello_init(VALUE obj, VALUE greeting)
{
HELLO(obj)->greeting = greeting;
return Qnil;
}

The VALUE represents the String object, which is what Ruby is interested
in. In that case, you should call rb_gc_mark(hello->greeting) so Ruby
can mark the String object, not the C string embedded in the object.

Regarding your new question, when you want to assign a new greeting
String object, just store the new VALUE in hello->greeting. The old
greeting String won't get marked during the next GC sweep, so GC will
know that it's available for collection.

(I hope this makes sense. It's so hard to make sense on Friday afternoon

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
irb require ... where does irb look? what path? anne001 Ruby 1 06-27-2006 12:07 PM
Memory loss in download of large files fixed in .NET 1.1 SP1? =?Utf-8?B?UGV0ZXJU?= ASP .Net 3 01-22-2006 09:41 AM
irb question - variable definitions when calling irb from a script problem Nuralanur@aol.com Ruby 1 10-26-2005 09:13 PM
[ANN] irb-history 1.0.0: Persistent, shared Readline history for IRB Sam Stephenson Ruby 1 06-18-2005 08:56 AM
JNI Memory Loss? Son KwonNam Java 1 06-15-2004 12:44 PM



Advertisments