acloudtree

Ruby, Rails 3.1, and the dreaded “Could not find a JavaScript runtime” error

While deploying a custom Rails 3.1 application I came across the following error.

Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

The fix was simple.

Add these gems to your application Gemfile

gem 'execjs'
gem 'therubyracer'

And run bundler.

$ bundle install

How To: Install The HomeBrew Package Management Utility Locally On Mac OSX

I was tired of having to compile my shiz by hand and not being a fan of Macports, I decided to give HomeBrew a try. Not knowing if I would like it, I wanted to install it locally in my home directory so that I could blow it away should it upset me.

Here are the steps I used to make it happen.

Assuming that you have git and all other dependancies (Intel CPU 2, OS X 10.5 or higher, Xcode with X11 3, Java Developer Update 4) installed.

jared@localhost$ cd
jared@localhost$ git clone https://github.com/mxcl/homebrew.git
jared@localhost$ vi ~/.bash_profile

Add the following line then :wq

export PATH=$HOME/homebrew/bin:$PATH

Open a new shell session and type brew.

jared@localhost$ brew
Example usage:
  brew install FORMULA...
  brew uninstall FORMULA...
  brew search [foo]
  brew list [FORMULA...]
  brew update
  brew upgrade [FORMULA...]
  brew [info | home] [FORMULA...]
 
Troubleshooting:
  brew doctor
  brew install -vd FORMULA
  brew [--env | --config]
 
Brewing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]
  open https://github.com/mxcl/homebrew/wiki/Formula-Cookbook
 
Further help:
  man brew
  brew home

You are all set!

 

 

Newbs, Ruby, Profiling Memory Leaks Using Memprof, And Web Scraping with Mechanize

So for the past four weeks in my spare time, I’ve been writing a sweet web scraping application for a personal research project. After overcoming the majority of regex and pattern matching challenges, I was finally able to get my spider a crawlin’ and then go to bed.

When I woke, I was excited to see what little bits of interesting information my application had found. But instead, I saw that it had dumped stack. And as a Ruby newb, here starts the long and arduous journey to understanding how to profile a ruby application. Yikes!

Definition of “Memory Leak” in Ruby

From what I have researched and my own personal experience. You (the developer) can’t really create a traditional memory leak.

Traditional Leak

  1. Allocate Memory (malloc, alloc)
  2. Assign Value (Object, string, int, etc…) To Memory
  3. No longer needing the value, you forget to release (dealloc) it
  4. The data stays in memory during the life of your application
  5. Particularly problematic when iterating or looping and assigning new values to memory

Ruby Memory Leak

  1. You assign a value to a variable
  2. You expect that the value has fallen out of scope
  3. Instead, it persists in memory as some Object you are unaware of is retaining it
  4. Not really noticeable in small applications or page requests where memory is freed at the end of the run cycle

Thats the basic difference (from my understanding). Please correct me if I am off on something.

Tips To Identify That You Have A Memory Leak

So a couple quick ways that you can tell you could have a memory leak.

Total Object Count

Try printing the total Object count to screen while your application is running. Should the count increase over time, this would indicate that you are retaining objects. Please note that you need to force Garbage Collection regularly to make sure that the total Object count is not just the logical growth cycle of your application.

Monitor Memory Usage

Monitor the Process I.D. (PID) of your application and look to see if memory usage increases over a given period of time. Should it continue to go up, this would strengthen the argument that you have a leak.

Again, these are two quick methods I used to come to the following point of action.

“Crap, it looks like something is going on with memory usage, I better take a closer look.”

Try forcing your Objects to release

I learned rather quickly, that by design, Ruby does not allow you to release or unset your Objects manually. So there ya go.

Set your Objects to nil to identify they are ready to be Garbage Collected

I tried this while grasping at straws, it was a dumb idea. I was taking shots in the dark and I wouldn’t advise going this route. What I needed was a really deep look at what Objects I was storing in memory so I could develop a story on why my application was leaking.

Memprof & its visualization partner Memprof.com

This is when I happened across the git repo Memprof by Joe Damato (@joedamato http://timetobleed.com). It was exactly what I needed.

Using Ruby 1.8.7, Install Memprof

$ gem install memprof

Signup for memprof.com

memprof.com (be aware, that when using this tool, it publishes your code to the web)

Include Memprof (at the VERY beginning of your application, just in case you want to profile your included gems too)

require `gem which memprof/signal`.chomp

Wrap Run Loop in Memprof.trace

def run_loop
  while next_link?(@browser.page) do
    Memprof.trace{
      iterate_search_page(@browser.page)
      click_next_link(@browser.page)
    }
  end
end

Run Your Application

$ ruby web_crawler.rb

Find Your Process ID of application

$ ps -ef | grep web_crawler.rb

After 5 minutes, Dump Trace To Memprof.com

$ memprof --pid 27890 --name elvis_dump2 --key YOUR_API_KEY

Analyze the heap visually with Memprof.com

Click On (objects with most outbound references)

On the far right, you will see an Object with the “Mechanize” class, and its length is 1081. This piece of information was critical in identifying that a Mechanize object I had instantiated was retaining a crap pile of objects.

What was the issue?

So it turns out that by default, Mechanize does NOT set a max_history=() value. What does this mean? Basically, my web crawler was storing every visited page, in memory, in entirety. BEWARE!

Hat tip ( @bradhe, @tim_linquist, @joedamato@elazar)

Note: My “Ruby Memory Leaks” definition is not entirely correct, but for the newbs out there reading this, I think trying to break it down further could potentially confuse you. If you want to understand Ruby memory allocation at some point, go watch this video by Joe Damato. (link)

Thoughts On Turning 30

Dear Gracie and Boy,

Today your Dad turned 30. In case I haven’t said it enough, you guys and your mom are the best things in my life. I am thankful for you on a daily basis.

I spent a lot of the day reflecting on how quickly time has passed and how much things have changed over the last decade. I wanted to take some time to point out some lessons I’ve learned in case you are curious about who your Dad is/was.

Be Ready To Work On Your Marriage

This decade I got married, and its been hard at times. Though it has gotten better and I love your mom more today than when we first met. Be ready to work hard and seek to find a partner who wants to work hard too.

Be Wary Of Debt

The USA is currently experiencing the worst recession/depression in history. In fact the world faces some serious challenges moving forward. From this vantage we the people are owned by Corporations.

We got to this point by taking on way too much debt, which blew up in our faces. Please be wary of taking it on yourself. Ask the people you respect for their opinions before jumping into anything that ends with you signing a promissory note.

Never trust a bank.

Be Strategic In Your Search

In your youth, responsibly try as many things as you can. Get a feel for what challenges, opportunities, and tasks await. Also, use that time to learn how to be your best advocate. But let this search be strategic to narrow the scope of what satisfies your heart. Sitting here at the big 3-0, I can tell you your youth goes by all too fast.

Be Cautious Of Anyone Or Thing Wanting You To “Act Now!!!”

The best things in life take hard work and the most satisfying things take a long time to come into fruition. I have found that the longer I wait for something, the better off I am. And in this consumerist society, I know my slow actions are not the norm, but I think thats a good thing.

Be Kind To Your Mother

I was not always kind to your Mom. My personality can just be too strong at times. But she has been patient with me. I actually don’t think I’ve ever met anyone nicer than your mom. She loves you all. Respect her, love her, emulate her where applicable.

Be Aware Of Death

Make sure to show the people that are in your life, that you love and appreciate them on a daily basis. Its easy to forget, but very important to do. Always be aware that the time you have with them is limited, so you need to try and make the most of it.

Be Forgiving

Things won’t always work out for you. The bad guy may even appear to win at times. But forgiveness is not about them, its about you. And a lot of peace can be found in forgiving others. The people who I look up to right now, are the best ones at forgiving.

Be Strong

Choose the right time and place to confront, speak to, or act out. I’ve learned the hard way, that there are times to keep my head down and look for a better opportunity for my energy to have maximum impact. This decade, I realized that if my emotion is HIGH, then more likely than not, it is the wrong time. But also know, there comes a day when you must act. Be strong.

Be Full Of Questions

You will never know it all. Don’t act like it. Don’t try to even freaking attempt it. Just ask questions and listen, listen all the time. You will be mocked for this. In school especially. But pay no attention to those doing the mocking. You will end up far wiser and smarter than everyone around.

Be Different

I have a general theory that the majority of people are just sad sheep. Indifferent and uncaring to most everything around them of any major value. Main stream media and religious organizations for the most part are just forms of propaganda and control. There is truth in each, but I’ve spent the later half of this decade trying to figure out how to disseminate the truth. To read between the lines.

So when you see the herd moving in one direction, you probably should be moving in the opposite one. But please don’t just move in reaction, in accordance with trying to be different. Use it as a starting point to question. Ask yourself “If everyone is going this way, dad warned me that I need to probably move the other, why?”.

You’ll figure it out.

Be Willing To Not Succeed

When you try, you’ll soon learn that you will not always succeed. But the people around you, for the most part, are one step behind, they have stopped trying. They are the definition of failure. So when you do get knocked down after trying something new, and the world laughs and tells you that you suck, just say;

“I’ve not failed, I’m just currently not succeeding”

And then get back up and try again.

* * *

I hope this helps you, and if you have any questions, let me know. I will only be a bedroom door or two away.

-love
dada

 

 

 

Copyright © Jared Folkins
Programming, Computers, Writing, Economics, and Life

Powered by WordPress