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.

26 Responses to “Ruby Memory Allocation”

  1. Charles Oliver Nutter  on August 9th, 2009

    The reason for the peculiar JRuby results is that since the JVM uses a generational, compacting garbage collector, it allocates large sections of memory at once and then partitions them as needed. The vast majority of objects created by the JVM use the heap and the GC. What you’re seeing here, I believe, is the memory allocated by native code outside the heap.

    This is also, of course, why the JVM has such fast object allocation rates; each thread gets its own buffer of memory to allocate as it sees fit, and once requested from the system it’s just a matter of biting off pieces. After the initial heap allocation, object allocation is “nearly free”.

    JVM engineers can feel free to correct me on any of this, but it gives you a rough idea what’s going on.

  2. admin  on August 9th, 2009

    Charles,

    Appreciate the comment – prompted further investigation and discovered the JVM allocates heap with mmap.

    Here’s the output of a barebones 4 line source file, assuming the mmap calls to be on JVM startup.

    I tried to intercept brk and sbrk syscalls as well, but assume they’d only be invoked for larger code bases that exceeds the initial heap size at runtime.

    - Lourens

  3. rkh (Konstantin Haase)  on August 10th, 2009

    Amazing blog post! Really enjoyed it.

  4. rammipisdibra  on March 4th, 2010

    http://presumesvideo6.blogspot.com/2010/03/video-mutilated-chiropteran.html

    fun

    download

    downloads

    games

    download.

  5. rammipisdibra  on March 5th, 2010

    downloads

    game game downloads videos free.

  6. Isaiah  on March 8th, 2010

    Incredible. blog.methodmjissing.ckm is amazing.

  7. scortions  on January 8th, 2011

    [url=http://www.loneworkeralarm.info]lone worker[/url]
    [url=http://www.loneworkeralarm.info]lone worker policies[/url]
    [url=http://www.loneworkeralarm.info]lone worker policy[/url]
    [url=http://www.loneworkeralarm.info]lone worker alarms[/url]
    [url=http://www.loneworkeralarm.info]lone worker alarm[/url]
    [url=http://www.loneworkeralarm.info]lone worker safety[/url]
    [url=http://www.loneworkeralarm.info]lone worker protection[/url]
    [url=http://www.loneworkeralarm.info]lone worker risk assessment[/url]
    [url=http://www.loneworkeralarm.info]lone worker systems[/url]
    [url=http://www.loneworkeralarm.info]nhs lone worker policy[/url]
    [url=http://www.loneworkeralarm.info]nhs lone worker[/url]

    The Health and Safety Executive (HSE) is frequently asked these questions. Working alone is not in itself against the law, and it will often be safe to do so. However, the law requires employers and others to think about and deal with any health and safety risks before people should be allowed to work alone.
    Employers have responsibility for the health, safety and welfare at work of all of their employees. They are also responsible for the health and safety of those affected by work activities, for example any self-employed people they engage and visitors such as contractors.
    These responsibilities cannot be transferred to any other person, including those people who work alone. It is the employer’s duty to assess risks to lone workers and take steps to avoid or control risks where necessary.
    Employees have responsibilities to take reasonable care of themselves and other people affected by their work activities and to co-operate with their employers in meeting their legal obligations.

    [url=http://www.loneworkeralarm.info]lone worker training[/url]
    [url=http://www.loneworkeralarm.info]lone worker monitoring[/url]
    [url=http://www.loneworkeralarm.info]lone worker system[/url]
    [url=http://www.loneworkeralarm.info]lone worker policy template[/url]
    [url=http://www.loneworkeralarm.info]lone worker system[/url]
    [url=http://www.loneworkeralarm.info]lone worker system[/url]
    [url=http://www.loneworkeralarm.info]hse lone worker[/url]
    [url=http://www.loneworkeralarm.info]lone worker legislation[/url]
    [url=http://www.loneworkeralarm.info]hse lone worker[/url]
    [url=http://www.loneworkeralarm.info]lone worker risk assessments[/url]
    [url=http://www.loneworkeralarm.info]lone worker device[/url]
    [url=http://www.loneworkeralarm.info]lone worker risk assessment example[/url]
    [url=http://www.loneworkeralarm.info]lone worker device[/url]
    [url=http://www.loneworkeralarm.info]hse lone worker policy[/url]
    [url=http://www.loneworkeralarm.info]lone worker alarm system[/url]
    [url=http://www.loneworkeralarm.info]lone worker procedure[/url]
    [url=http://www.loneworkeralarm.info]identicom lone worker[/url]
    [url=http://www.loneworkeralarm.info]argyll lone worker[/url]
    [url=http://www.loneworkeralarm.info]lone worker panic alarm[/url]
    [url=http://www.loneworkeralarm.info]lone worker regulations[/url]
    [url=http://www.loneworkeralarm.info]lone worker panic alarm[/url]
    [url=http://www.loneworkeralarm.info]lone worker protection systems[/url]
    [url=http://www.loneworkeralarm.info]romad lone worker[/url]
    [url=http://www.loneworkeralarm.info]lone worker software[/url]
    [url=http://www.loneworkeralarm.info]lone worker safety training[/url]
    [url=http://www.loneworkeralarm.info]lone worker software[/url]
    [url=http://www.loneworkeralarm.info]lone worker guidance[/url]
    [url=http://www.loneworkeralarm.info]lone worker safety 2010[/url]
    [url=http://www.loneworkeralarm.info]lone worker safety solutions[/url]
    [url=http://www.loneworkeralarm.info]guardian 24 lone worker[/url]
    [url=http://www.loneworkeralarm.info]lone worker safety policy[/url]
    [url=http://www.loneworkeralarm.info]lone worker alarm uk[/url]
    [url=http://www.loneworkeralarm.info]lone worker monitoring system[/url]

  8. Rapidshare Aylin  on February 5th, 2011

    Hm. im out right now.

  9. Sharyn Menapace  on May 23rd, 2011

    Usually I do not post on posts, but I may prefer to say which that site truly pressured me to get it done! Thanks, really great post.

  10. Marlin Kayser  on June 1st, 2011

    Search engines disregard or penalize cloaking, hidden text, key terms meta tags, key phrase denseness stuffing, irrelevant links, unnatural hyperlinks, hyperlink exchanges, website link farms, web jewelry, and many others.

  11. Nhwzobis  on September 6th, 2011

    Go travelling top lolita
    91206

  12. Crzqusvm  on September 7th, 2011

    I support Manchester United underage teens
    99389

  13. Gzweyxpa  on September 8th, 2011

    I’m doing a phd in chemistry nn lolita
    mshmnc

  14. Yzawictd  on September 30th, 2011

    I wanted to live abroad guestbook lolita lo bbs 24013

  15. Annorjovan  on December 10th, 2011

    must look at this online shopping , just clicks away

  16. Eonqnqfg  on December 14th, 2011

    Where are you calling from? Lolita Top Sites
    >:DD

  17. Ewwcrmkl  on December 14th, 2011

    I’d like to pay this cheque in, please Lolita Biz
    lbwvja

  18. Zvxkhtcb  on December 15th, 2011

    I’d like to send this parcel to Lolitas Castle
    96253

  19. Iqvvxecl  on December 15th, 2011

    Your account’s overdrawn Lolita Cum
    587

  20. Dxmovqha  on December 16th, 2011

    Could you give me some smaller notes? Preteen Loli Porn
    %PPP

  21. Btkjyhff  on December 16th, 2011

    I work for a publishers Preteen Nn
    534095

  22. Gwpwybff  on December 16th, 2011

    I’m from England Preteen Tgp
    lvsaiu

  23. Qghpzbfs  on December 16th, 2011

    Would you like a receipt? Non Nude Preteen
    2619

  24. Nbvksunw  on January 3rd, 2012

    How would you like the money? Pthc Cp Toplist
    >:-DD

  25. Uaopsgrc  on January 3rd, 2012

    Through friends Teenage Nymphets Garden
    29254


Leave a Reply