C++ Change Max RAM Limit
Asked Answered
S

4

4

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.

Swansea answered 3/11, 2011 at 15:34 Comment(4)
you need to specify exactly what limit you are running into before we can start guessing how to lift the limitAsia
Why would 100k objects result in out of memory? How big is each object? Are you perhaps in a 32 bit process and its address space rather than memory that is the limit?Liftoff
@Precursor: How much memory is the program using when it "runs out"? How much RAM/swap do you have? Is the program 32 or 64 bit?Vaudois
100k objects at 10kb each is still only 1gig. What is your limit set to? How much RAM do you have? and how big are these objects?Corrinnecorrival
H
3

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?

Hypnotist answered 3/11, 2011 at 16:6 Comment(0)
S
2

If these limits are being imposed on you by the system administrator, then no - you're stuck. If you're ulimiting yourself, sure - just raise the soft and hard limits.

As pointed out by Chris in the comments, if you're a privileged process, you could use setrlimit to raise your hard limit in process. However, one presumes if you're under a ulimit, then you're unlikely to be a privileged process.

Spates answered 3/11, 2011 at 15:40 Comment(5)
Well, I am the system administrator. But the thing is, I don't really want to use the ulimit command on linux to change it globally. Is there some way I can do this from code?Swansea
You can use the ulimit command only inside a single shell, i.e. inside a single terminal.Swinish
You could launch through a shell that isn't under the limit. But there's no userland API to bypass it (it would be a bit useless if any application not wanting to be limited could just "opt out").Spates
@Adam: there is a userland API -- setrlimit(2). The ulimit command is basically just a trivial wrapper around setrlimit. Of course, you may get EPERM errors if you don't have permission.Comedienne
@ChrisDodd Ah, my mistake. Though my main point stands - non privileged processes can't change their hard limit upwards.Spates
S
2

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.

Swinish answered 3/11, 2011 at 15:44 Comment(2)
This isn't a memory leak. I must allocate around 100,000 instances of a class, hence the fact I am running out of memory.Swansea
Then the solution is either to buy some more RAM (which is cheap today), or lower the number of instances, or decrease the size of each instance.Swinish
H
1

If you have permissions you can use the setrlimit call (which supersedes vlimit) to set the limits at the start 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
  } 
} 
Heinrike answered 3/11, 2011 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.