Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate memory allocation errors

Tags:

My C application uses 3rd libraries, which do their own memory management. In order to be robust, my application has code to deal with failures of library functions due to lack of free memory.

I would like to test this code, and for this, I need to simulate failures due to lack of memory.

What tool/s are recommended for this? My environment is Linux/gcc.

like image 604
Omer Zak Avatar asked Sep 20 '08 18:09

Omer Zak


People also ask

What is memory allocation error?

A memory allocation (malloc) failure occurs when the active controller does not have enough memory to run a stack unit. This failure may occur if you configure a large number of VLANs (for example, 4000) or STP instances (for example, 255) in the router image.

What are a few potential consequences of poor memory allocation?

Symptoms. Applications that frequently allocate memory may experience random "out-of-memory" errors. Such errors can result in other errors or unexpected behavior in affected applications.

What is allocation error?

Allocation Error Panel (ISRUADCS) The term inconsistent attributes refers to incompatible values that have been specified for one or more of these items: Space units; Primary or Secondary quantity; Directory blocks; Record format; Record length; Block size.

What is the problem of using dynamic memory allocation?

The problem with dynamic memory allocation is that it is not deallocated itself, developer responsibility to deallocate the allocated memory explicitly. If we cannot release the allocated memory, it can because of memory leak and make your machine slow.


2 Answers

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.

like image 112
freespace Avatar answered Sep 22 '22 16:09

freespace


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.

like image 34
Vinko Vrsalovic Avatar answered Sep 20 '22 16:09

Vinko Vrsalovic