Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a program to appear to run out of memory?

I have a C/C++ program that might be hanging when it runs out of memory. We discovered this by running many copies at the same time. I want to debug the program without completely destroying performance on the development machine. Is there a way to limit the memory available so that a new or malloc will return a NULL pointer after, say, 500K of memory has been requested?

like image 880
jwhitlock Avatar asked Aug 04 '09 18:08

jwhitlock


People also ask

Can a program run out of memory?

So what happens when your computer runs out of RAM? The easy answer to that question: Your computer will start to work less efficiently. That's because when your computer runs out of memory it will start to use the hard drive space for "virtual memory" to compensate.

What does it mean when Mac says system has run out of application memory?

When running an app on Mac, if all physical RAM are used, macOS will create swap files, which takes up disk space. So, it takes up more and more memory until your hard drive is full and there is no room for the swap files, the system will report that it has run out of application memory.


2 Answers

Try turning the question on its head and asking how to limit the amount of memory an OS will allow your process to use.

Try looking into http://ss64.com/bash/ulimit.html

Try say: ulimit -v

Here is another link that's a little old but gives a little more back ground: http://www.network-theory.co.uk/docs/gccintro/gccintro_77.html

like image 150
chollida Avatar answered Oct 09 '22 03:10

chollida


One way is to write a wrapper around malloc().

static unsigned int requested =0;  void* my_malloc(size_tamount){     if (requested + amount < LIMIT){        requested+=amount;        return malloc(amount);    }     return NULL } 

Your could use a #define to overload your malloc.

As GMan states, you could overload new / delete operators as well (for the C++ case).

Not sure if that's the best way, or what you are looking for

like image 34
Tom Avatar answered Oct 09 '22 04:10

Tom