[FATAL] failed to allocate memory

393 views
Skip to first unread message

Satya Rao

unread,
Feb 23, 2017, 5:50:17 AM2/23/17
to rspec
Hi Everybody,
 I'm new to Rspec. I'm getting '[FATAL] failed to allocate memory' when I loop through some it blocks over 500000 times. And when tried the same with pure ruby I'm not getting the fatal error and the execution was successful. Below is my code.

Rspec code:

fatal_error_check_spec.rb
--------------------------------------

describe "fatal error check" do
for i in 1 .. 500000
it "Example it block 1" do
puts "My first it"
end
it "Example it block 2" do
puts "My second it"
end
end
end



When running the above rspec code  gives me an above fatal error after ~4 mins without running anything.


Pure Ruby code:

fatal_error_check.rb
-----------------------------

for i in 1 .. 500000
puts "My first it"
puts "My second it"
end

When running this ruby code, I didn't get any error and printed those two puts statements 5 lack times successfully.


Please help me with solving the above fatal error issue with Rspec. What should I do to fix this error.


My system env is as follows:
-----------------------------------------
Windows 7 PC qith 8GB RAM
Ruby version: 1.9.3
rspec (3.5.0)


Please help me to fix this issue.


Thanks,
Satya.

Jon Rowe

unread,
Feb 23, 2017, 5:52:53 AM2/23/17
to rs...@googlegroups.com
You’re getting that error because you’re running out of memory, the two code snippets you describe are not identical, the RSpec one creates more classes and objects under the hood, you should reduce the amount of specs you are trying to create at once or make more memory available to Ruby.

Hope that helps

Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/c2824e72-abb7-4374-b2a9-7137acad9aea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Satya Rao

unread,
Feb 23, 2017, 6:03:40 AM2/23/17
to rspec
Thanks Jon for your quick reply, what I observed with rspec code is, first it tries to loop over 5 lack times and then try to run it blocks, it means the looping is actually happening twice i.e 10 lack times, is that a rspec a limitation. Is there any other solution other than increasing the RAM? 

And then Rspec is not suitable for stress scenarios?

Jason Fleetwood-Boldt

unread,
Feb 23, 2017, 7:58:26 AM2/23/17
to rs...@googlegroups.com
Rspec is picky about the "describe" and "it" blocks. Those are methods being called, and as such, create your specs in a "pre-run" place in memory. Generally you should avoid any Ruby code outside of an "it" block (of course, except for the provided Rspec hooks— before(:each), before(:all), etc) as this often causes problems.

First of all, you're not even supposed to have 2 specs with identical names (although you could fix that easily by putting the loop variable 'i' inside of your spec name, like "Example it block for #{i} iteration")

Second of all, you're actually telling Rspec to create 50,000 specs. (Since you two example blocks, actually 100,000 specs). Are you insane? There's no way I could possibly get my Mac — which admittedly is no longer top of the line — to run 100,000 specs in memory.

Why would you run the same spec 100,000 times? What you're trying to do does not make any sense.

Now, perhaps you want to loop through some operation 50,000 times. In this case, you'd have 1 spec that would have the for loop inside of it, not the "it" inside of the for loop which is your problem.

And the short answer is : Rspec is a testing tool for Ruby code, specifically domain logic. As you say, it is not suitable for stress testing or load testing. For load or stress testing, you'll almost certainly want to do this in a Production environment (or a Staging environment, assuming you have Prod-Staging parity) as you'l need to load/stress test against the real architecture you're running on. 

I've never heard of anyone using Rspec for load or stress testing. Your mileage may vary, and perhaps what you're trying to do is use Rspec to hit an external site (like your Production site), which makes slightly more sense. But still, I see no reason why the for loop is outside of the "it" statement. 

Rspec does not run procedurally as the code might suggest (and as many, many junior Ruby people get confused by), it creates the specs first by parsing the whole file ("describe" and "it" are actually methods being called to create specs in memory.), then it runs all the specs after having parsed them. The reason you see it crash without running anything is that you've just maxed out your memory before you've even run your very first spec. All it's doing is maxing out while parsing the file and creating the specs (100,000 of them) in memory. 

-Jason





For more options, visit https://groups.google.com/d/optout.

If you'd like to reply by encrypted email you can find my public key on jasonfleetwoodboldt.com (more about setting GPG: https://gpgtools.org

Allen Madsen

unread,
Feb 23, 2017, 8:53:36 AM2/23/17
to rs...@googlegroups.com
This is probably closer to what you want. https://github.com/piotrmurach/rspec-benchmark
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.

To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/ea6d9921-0bb7-4126-bdca-fcad936c2215%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
If you'd like to reply by encrypted email you can find my public key on jasonfleetwoodboldt.com (more about setting GPG: https://gpgtools.org

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.

To post to this group, send email to rs...@googlegroups.com.

Satya Rao

unread,
Feb 23, 2017, 9:34:35 AM2/23/17
to rspec
Thanks Jason for your reply.
Let me explain what I'm trying to do here. In my rspec script I have 5 examples(i.e. 5 it blocks) under 1 describe block which I need to repeat all these 5 examples sequentially 5 lach times to test the stability of my device under test. this is my requirement. What is the best possible way to do this?

Thanks,
Satya.

Jason Fleetwood-Boldt

unread,
Feb 23, 2017, 10:05:40 AM2/23/17
to rs...@googlegroups.com
I would still put the loop inside the it blocks, so you're just running a few actual specs, and then running your loop just to test the actual thing you're testing.

Otherwise you're just running the same spec again and again, each time doing Rspec's teardown and setup procedure, which will surely reset the state of your app & database to its original state, so it seems very counter-intuitive to me to do something like that. It's like you're just hitting your head against a wall and then wondering why your head hurts. At least that what it seems like to me, but I don't have the full context of what you're trying to achieve. 

Seem's like Allen Madsen's answer with a link https://github.com/piotrmurach/rspec-benchmark is a good option for benchmarking. (Which is actually distinct from load and stress testing)




On Feb 23, 2017, at 9:34 AM, Satya Rao <sat...@gmail.com> wrote:

Thanks Jason for your reply.
Let me explain what I'm trying to do here. In my rspec script I have 5 examples(i.e. 5 it blocks) under 1 describe block which I need to repeat all these 5 examples sequentially 5 lach times to test the stability of my device under test. this is my requirement. What is the best possible way to do this?

Thanks,
Satya.

Aaron Kromer

unread,
Feb 23, 2017, 10:06:46 AM2/23/17
to rs...@googlegroups.com

Let me explain what I’m trying to do here. In my rspec script I have 5 examples(i.e. 5 it blocks) under 1 describe block which I need to repeat all these 5 examples sequentially 5 lach times to test the stability of my device under test. this is my requirement. What is the best possible way to do this?

Create a new single spec specifically for this.

In general it is advised that specs are not order dependent. Meaning each spec should be able to run, testing what is expected, in isolation (i.e. by itself). Having spec order dependencies general is an indication of global state leak and is a bad thing. This is why RSpec sets up the default configuration to set random spec run order.

With that being said, I have written similar “stress” tests specifically with regard to threading. It can be hard to ensure a known thread failure path is hit so often things are run in a loop, within a single spec, to ensure enough workload to produce a high probability of issue.

Jon Rowe

unread,
Feb 23, 2017, 5:31:49 PM2/23/17
to rs...@googlegroups.com
I’d say https://github.com/piotrmurach/rspec-benchmark is exactly what you want as you can specify your performance requirements in either direct timing or IPS, and should be cleaner memory wise.

Jon Rowe
---------------------------

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.

Satya Rao

unread,
Feb 24, 2017, 1:39:04 AM2/24/17
to rspec
Thank you so much all for your quick response on this, yes I will try out these options.

Thanks,
Satya.
Reply all
Reply to author
Forward
0 new messages