Ruby Memory Allocation

During a recent discussion at work, my ever so sharp coworker James Tucker noted the significant void between user code and low level optimizations.

Micro benchmarks and rubyspec compliance are frequently referenced to compare implementations and quite often cited in random rants as well.What happens at that intermediate, libc, level ?

In this post we’ll explore the ISO C memory allocation / deallocation functions as invoked by the following interpreters :

* ruby-1.8.7-p72 (./configure)
* ruby-1.9.1-p129 (./configure)
* jruby-1.3.1 (binary distribution)

Requirements

* A DTrace enabled OS (OS X Leopard, Solaris etc.)
* One or more interpreters installed
* A local checkout of extlib

I’ve opted to NOT test with the latest 1.9.2 preview – see why

The DTrace PID provider

A set of C level probes is spawned for the lifetime of a given command or existing process.When the process dies, the probe’s is unset.You don’t have to recompile or relink – it just works.See the Sun wiki for further information.

This is very different from static probes that ships with MySQL 6 or a previously released patched tree by Joyent.

Memory Allocation

Regardless of the underlying GC implementation, there’s three ISO C functions that handle memory allocation.

* malloc allocates X bytes of memory
* calloc allocates memory for X objects of Y size each
* realloc shrinks or expands the size of a previously allocated memory region

Each allocation function returns a pointer to the allocated (or reallocated) memory region which can be released with the free function call.

Of note is that most implementations never release memory from the heap back to the underlying OS – it’s assigned to a pool for reallocation to reduce system call (sbrk) overhead of the current process.

Toolbox

Ruby 1.8.7

I happen to use this as my stock 1.8 series interpreter – 1.8.6 should yield similar output.

Ruby 1.9.1

JRuby 1.3.1

Again, I’ve noticed a 1.4 version flying around, but happened to have 1.3.1 installed.

Passing Thoughts

Note the liberal memory allocation overheads on the 1.8 series, but an otherwise overall similar distribution pattern to 1.9.I can’t comment much on the JVM case due to limited Java experience.Anyone care to chime in ?

This is an exercise to test the waters for a potential audience for these experiments.I have a few contexts lined up, with a specific MRI focus, but can get into the bowels of most things.Suggestions and thoughts welcome.

RailsWayCon

I recently got out the comfort zone to present at a tech conference, RailsWayCon for the first time.Public speaking being somewhat nerve-wracking for most, especially given most developers are borderline autistic, things went relatively smoothly.

Here follows a quick rundown of each talk as well as embedded slides.Source code and slides have also been made available on my Github account.

Embracing Events

An in depth overview of Event-driven Programming and how the Operating System can help with that.Topics touched upon include :

  • Context Switches ( Threads vs Fibers )
  • Scheduling ( Cooperative vs Preemptive )
  • Programming Models ( Evented vs Batched )
  • Frameworks ( Eventmachine + Neverblock )
  • Operating System nofication ( Signals + POSIX realtime )
  • Practices from Online Gaming ( Project Darkstar )
  • Testing Event-driven Code

Perhaps touching on gaming at a Rails conf was inappropriate, but I foresee significant production use of small mountable apps around core Rails infrastructure in the near future.Lightweight processes are no different than tasklets.

I’m looking to spend some time in the coming weeks on the JRuby wrap of the Project Darkstar Server module and hope that’ll at the very least inspire similar projects and advocate production use of JRuby.

Special thanks to Muhammed Ali for the last minute diagrams.

Inside Matz Ruby

Very much challenged by a lack of time ( 20 minutes !!! ) one had to touch on a handful of interesting concepts, but it sort of came together as a “patched” ( pun intended ) talk jumping from class representations to threading to gc.

As per suggestion from wycats, watch out for a series of “play along at home” posts that focus on the MRI interpreter.To raise the game, I’ll try my best to have them VM agnostic ( 1.8 and 1.9 ).

Thank You’s

It’s been a great week – no fuzz, no drama.Lotta coffee, little sleep, lotta code.I’d like to take a moment and reflect on the hospitality of the following individuals / companies, in addition to the RailsWayCon team.

Mathias Meyer and Jonathan Weiss

Recommendations for meals, coffee, transport, being loony and making time available from busy schedules, and families.The definite deployment and scaling experts through Peritor, do watch out for a unique EC2 product to be launched soon.

Alex, Thilo && Jan at Upstream Agile

Great office space for co-working and an ample supply of Club-Mate.Alex and Thilo recently built DoingText and Jan’s working on a stealth Couch DB project.

Sven Fuchs && Clemens Kofler

Localization and i18n experts.When not co-working with Clemens and the 90 dB his macbook fan emits, Sven specializes in dancing after 02:30am.