Black Bytes
Share this post!

What’s Happening in Your Ruby Application?

What would you do if you wanted to know what’s going on with your Ruby application?

In Ruby we don’t have fancy tools like Java, but we have the ObjectSpace module which can give you some information about the current state of your application.

Counting Objects

Using ObjectSpace you can know what objects are currently ‘alive’ in your program.

What does it mean for an object to be alive? An object is alive as long as it has any references pointing to it. A reference is just a way to access the object, like a variable or a constant. If an object can’t be reached then it means that it’s safe to be removed from memory.

Example:

Now let’s see an example of ObjectSpace in action:

This will print a table with the object count for your top-10 classes.

If you suspect of a memory leak you could log this data every hour & find out if there is some object count that keeps increasing all the time but never goes down.

Fun with Objects

When using ObjectSpace you get access to the actual objects, not just information about them, so you can do some fun things like printing the value of all the strings or printing the path of all your File objects.

Example:

This will print all the in-memory strings, sorted by size. You will notice that there are many strings that you didn’t create yourself, they are created by the Ruby interpreter.

Practical uses? Well, this is mostly for debugging & gathering stats about your app 🙂

Object Memory Size

Another thing you can do is to use ObjectSpace.memsize_of to find the memory size of a particular object.

Example:

One thing to keep in mind is this warning from the documentation:

“Note that the return size is incomplete. You need to deal with this information as only a HINT.”

If you try this method with different types of objects you will find some interesting things, like Fixnums always returning 0.

The reason for this is that Ruby doesn’t internally create Fixnum objects, you can learn more about this on the post I wrote about numbers in Ruby.

Another interesting one are strings:

I use "A" * size as a way to create a longer string without having to type it out 🙂

Wait! What did just happen? Well, it turns out that Ruby has a built-in optimization for strings smaller than 24 characters, that’s why there is a jump in memory use after that. You can see this in more detail in this post from Pat Shaughnessy.

Finding Aliased Methods

Wouldn’t it be nice if there was a ‘master’ list of all the aliased methods in Ruby?

Wish granted! Take a look at this:

I got this code from a Stackoverflow answer. It defines an aliased_methods method on the Module class, which uses the instance_methods method to get a list of all the instance methods defined on a class.

I know that may sound a bit confusing, but that’s metaprogramming for you!

Here is the rest of the code, which builds an array of all the class names that have at least one ‘alive’ object, then it calls aliased_methods on every class & prints the output.

This is what the output looks like:

Conclusion

I hope you enjoyed learning about the cool things you can do with ObjectSpace, now go try it out and let me know if you find anything interesting!

Don’t forget to share this post with all your programmer friends, it will help them learn something new & it will help me get more readers 🙂

7 comments
Sriman says 10 months ago

Nice tip… thanks for sharing.

    Jesus Castello says 10 months ago

    Thanks for reading 🙂

Min Soo Kim says 10 months ago

Another good one. Thank you for good stuff!

    Jesus Castello says 10 months ago

    Thank you for reading 🙂

Saurabh Sikchi says 10 months ago

Awesome Post!

    Jesus Castello says 10 months ago

    Thank you & thanks for sharing on social networks 🙂

Hugo says 9 months ago

Thanks.

I had fun and found: ObjectSpace.dump_all ;-p lots of things to see for a curious mind 😀

Comments are closed