How to simulate memory allocation errors How to simulate memory allocation errors linux linux

How to simulate memory allocation errors


You can use ulimit to limit the amount of resources a user can use, including memory. So you create a test user, limit their memory use to something just enough to launch your program, and watch it die :)

Example:

ulimit -m 64

Sets a memory limit of 64kb.


Create your own malloc wrapper which will randomly return null instead of a valid pointer. Well, or which fails consistently if you want to unit test.


On operating systems that overcommit memory (for example, Linux or Windows), it is simply not possible to handle out-of-memory errors. malloc may return a valid pointer and later, when you try to dereference it, your operating system may determine that you are out of memory and kill the process.

http://www.reddit.com/comments/60vys/how_not_to_write_a_shared_library/ is a good write-up on this.