Reputation: 88248
A very similar question was asked for the .net
Windows environment:
How do you protect yourself from runaway memory consumption bringing down the PC?
This question is posed for the Unix environment using C or C++. As an example, consider the dangerous little block below:
#include <vector>
int main() {
int n;
std::vector<double> A(n);
}
If you're lucky the code will throw a range error. If you're unlucky (in my case the value of a in memory was 283740392) the code will quickly use all the available RAM and cause massive swapping to disk, grinding the OS to a virtual standstill faster then it can be killed. The process can always be killed eventually of course, but it may often take minutes to recover as all the other running processes have to be loaded back into memory. This is not a question whose answer implies a lack of RAM, one could easily give a runaway process that swamps any available machine.
Upvotes: 2
Views: 235
Reputation: 1847
In linux there is something called 'the OOM killer'. It kills processes based on memory usage and history of cpu usage.
Upvotes: 1
Reputation: 66981
A possibly better but more complicated answer (than ulimit) would be a custom allocator, that limits the maximum size of any given allocation (100MB or so), but does not limit the total memory allowed to be used..
Upvotes: 0
Reputation: 96311
As suggested in a comment, either ulimit -v <size>
before you start your program, or setrlimit
programmatically within it.
Upvotes: 5