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

只愿长相守 提交于 2019-12-29 16:28:14

问题


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?


回答1:


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




回答2:


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




回答3:


  • Which OS? For Unix, see ulimit -d/limit datasize depending on your shell (sh/csh).

  • You can write a wrapper for malloc which returns an error in the circonstance you want. Depending on your OS, you may be able to substitute it for the implementation's one.




回答4:


That depends on your platform. For example, this can be achieved programmatically on Unix-like platforms using setrlimit(RLIMIT_DATA, ...).

EDIT:

The RLIMIT_AS resource may also be useful in this case as well.




回答5:


Override new and new[].

void* operator new(size_t s)
{
}
void* operator new[](size_t s)
{
}

Put your own code in the braces to selectively die after X number of calls to new. Normally you would call malloc to allocate the memory and return it.




回答6:


An other way of doing it is to use failmalloc which is a shared library that overrides malloc etc. and then fail :-). It gives you control over when to fail and can be made to fail randomly, every nth time etc.

I havent used it my self but have heard good things.




回答7:


I once had a student in CS 1 (in C, yeah, yeah, not my fault) try this, and ran out of memory:

int array[42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42][42]..... (42 dimensions);

and then he wanted to know why it gave errors...




回答8:


If you want to spend money, there's a tool called Holodeck by SecurityInnovations, which lets you inject faults into your program (including low memory). Nice thing is you can turn stuff on and off at will. I haven't really used it, much, so I don't know if it's possible to program in faults at certain points with the tool. I also don't know what platforms are supported...




回答9:


As far as I know, on Linux, malloc will never return a null pointer. Instead, the OOM Killer will get called. This is, of course, unless you've disabled the OOM Killer. Some googling should come up with a result.

I know this isn't your actual question, but it does have to do with where you're coming from.



来源:https://stackoverflow.com/questions/1229241/how-do-i-force-a-program-to-appear-to-run-out-of-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!