Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Change Max RAM Limit

How would I change the max amount of RAM for a program? I constantly am running out of memory (not system max, ulimit max), and I do not wish to change the global memory limit. I looked around and saw the vlimit() function might work, but I'm unsure exactly how to use it.

Edit: I'm on linux 2.6.38-11-generic This is not a memory leak, I literally must allocate 100k of a given class, no way around it.

like image 418
Precursor Avatar asked Nov 07 '25 15:11

Precursor


2 Answers

Do you allocate the objects on the stack and are in fact hitting the stack limit?

Do you, e.g., write something like this:

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

This array will be allocated on the stack. A better solution -- which automates heap allocation for you -- would be to write

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

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

How to increase the gcc executable stack size?

Can you set the size of the call stack in c++? (vs2008)

And you might want to consider:

When do you worry about stack size?

like image 157
Sebastian Avatar answered Nov 10 '25 06:11

Sebastian


Do you understand why you are hitting the RAM limit? Are you sure that you don't have memory leaks (if you do have leaks, you'll need more and more RAM when running your application for a longer time).

Assuming a Linux machine, you might use valgrind to hunt and debug memory leaks, and you could also use Boehm's conservative garbage collector to often avoid them.

like image 29
Basile Starynkevitch Avatar answered Nov 10 '25 08:11

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!