MrRobot
MrRobot

Reputation: 21

How can I cause a Memory Error in python 2.7?

I'm working on a system that has about 128KB of RAM, one of my scripts occasionally causes a ERRNO 12 Cannot Allocate Memory error.

I have a few solutions I want to test.

But how can I replicate the problem when it seemingly happens randomly once a day?

Any bad scripts that will cause ERRNO 12 Cannot Allocate Memory error?

Most posts are trying to solve Memory errors, I want to cause one to test the robustness of my code.

Upvotes: 1

Views: 1137

Answers (1)

Tom Karzes
Tom Karzes

Reputation: 24062

If you want to force Python to run out of memory, this should make it happen very quickly regardless of how much memory is available:

x = [None]
while True:
    x += x

This will double the length of x on every iteration until it fails.

Upvotes: 3

Related Questions