C ++ Changing the maximum RAM limit

How can I change the maximum amount of RAM for a program? My memory is constantly running out (not the system maximum, ulimit max), and I do not want to change the global memory limit. I looked around and saw that the vlimit () function might work, but I'm not sure how to use it.

Edit: I'm on linux 2.6.38-11-generic This is not a memory leak, I literally have to allocate 100k of this class, in no way get around this.

+3
source share
4 answers

Do you allocate objects on the stack and actually push the stack constraint?

You, for example, write something like this:

void SomeFunction() { MyObject aobject[100000]; // do something with myobject } 

This array will be allocated on the stack. The best solution that automates heap allocation for you is to write

 void SomeFunction() { std::vector<MyObject> veccobject(100000); // 100.000 default constructed objects // do something with myobject } 

If for some reason you really need a large stack, consult your compiler documentation for the appropriate flag:

How to increase the size of the gcc executable stack?

Can you set the size of the call stack in C ++? (VS2008)

And you might think:

When are you worried about stack size?

+3
source

If these restrictions are imposed on you by the system administrator, then no - you are stuck. If you ulimit yourself, of course, just raise the soft and hard limits.

As Chris pointed out in the comments, if you are a privileged process, you can use setrlimit to raise your hard limit in the process. However, it is assumed that if you are under ulimit , then you are unlikely to be a privileged process.

+2
source

Do you understand why you click the RAM limit? Are you sure that you do not have memory leaks (if you have leaks, you will need more and more RAM when you run the application for a longer time).

Assuming a Linux machine, you can use valgrind to find and debug memory leaks, and you can also use Bem's conservative garbage collector to avoid them often.

+2
source

If you have permissions, you can use the setrlimit call (which replaces vlimit) to set limits at the beginning of your program. This will only affect this program and its offspring.

 #include <sys/time.h> #include <sys/resource.h> int main() { struct rlimit limit; limit.rlim_cur = RLIM_INFINITY; limit.rlim_max = RLIM_INFINITY; if (0 != setrlimit(RLIMIT_AS, &limit)) { //errro } } 
+1
source

All Articles