Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, This is my first post and I hope I'm using the correct forum, otherwise please point me to the correct one. I will like to know how I can ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    3

    Question Limit Application's Memory usage with C - How to?

    Hello,

    This is my first post and I hope I'm using the correct forum, otherwise please point me to the correct one.

    I will like to know how I can control or set a limit on the amount of memory that my program will use. For example if I set a limit of 500MB (via getopt) I will like the program to not allocate above 500MB. There are some complications because I link against a couple of libraries that might have their own malloc/free implementations but if I can't control them at least I will like to control the rest of the memory allocations n my application.

    My application is memory intensive and might crash the system as it keeps consuming memory, I like to set that limit so that no segfault happens and also be able to work around when the limit is reached.

    I'm using Fedora 11, gcc, etc from the standard distribution on a 32bit platform.

    Thank you in advance.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    See the man page for setrlimit(). That should do what you want.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    3
    I was thinking on using that function (setrlimit())but I read about the stack expansion and doubted it to solve the problem. I will have to find more info on the alternate stack topic. I'm not sure if the stack is strongly related to using recursive calls, if that is the case I might not have such problem. Is there a way to measure how much stack my program uses?

    Thank you.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    All automatic variables and function calls are on the stack, so recursion will consume stack. Unless you have a lot of recursion in functions that have a lot of big local variables, usually stack isn't much of an issue. I use recursion a lot (there are classes of programming problems that are way too difficult to solve otherwise), but if you are blowing a 500MB stack segment, then you are doing something wrong. Usually, this would be indicative of a bug that results in an infinite recursion, and even if you allow unlimited stack expansion, it will eventually burn up all available memory and crash the system in a very nasty way. This is why I don't recommend allowing unlimited stack expansion in any application (or server) - I'd rather crash the application with a memory exception than crash the operating system and possibly nuke my file systems.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...