3

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.

Precursor
  • 622
  • 7
  • 18
  • you need to specify exactly what limit you are running into before we can start guessing how to lift the limit – sehe Nov 03 '11 at 15:37
  • 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? – David Heffernan Nov 03 '11 at 15:54
  • @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? – Mooing Duck Nov 03 '11 at 16:00
  • 1
    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? – jkerian Nov 03 '11 at 16:14

4 Answers4

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?

Community
  • 1
  • 1
Sebastian
  • 4,802
  • 23
  • 48
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.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • 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? – Precursor Nov 03 '11 at 15:42
  • You can use the `ulimit` command only inside a single shell, i.e. inside a single terminal. – Basile Starynkevitch Nov 03 '11 at 15:45
  • 4
    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"). – Adam Wright Nov 03 '11 at 15:46
  • 1
    @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. – Chris Dodd Nov 03 '11 at 16:31
  • @ChrisDodd Ah, my mistake. Though my main point stands - non privileged processes can't change their hard limit upwards. – Adam Wright Nov 03 '11 at 16:34
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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 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. – Precursor Nov 03 '11 at 15:46
  • 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. – Basile Starynkevitch Nov 03 '11 at 16:08
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
  } 
} 
Paul Rubel
  • 26,632
  • 7
  • 60
  • 80