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

后端 未结 9 911
旧巷少年郎
旧巷少年郎 2020-12-24 07:39

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 co

相关标签:
9条回答
  • 2020-12-24 08:12

    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.

    0 讨论(0)
  • 2020-12-24 08:18

    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.

    0 讨论(0)
  • 2020-12-24 08:22

    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.

    0 讨论(0)
提交回复
热议问题