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

后端 未结 9 910
旧巷少年郎
旧巷少年郎 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 07:57

    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

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

    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.

    0 讨论(0)
  • 2020-12-24 08:04
    • 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.

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

    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

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

    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...

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

    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...

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