How to restrict memory usage of a c++ program

This is a discussion on How to restrict memory usage of a c++ program within the C++ Programming forums, part of the General Programming Boards category; How can i restrict the total memory usage of a program. i have to check certain code but i want ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    Question How to restrict memory usage of a c++ program

    How can i restrict the total memory usage of a program.
    i have to check certain code but i want to restrict the rem used by the program when running.
    please help me out

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Same answer (and question) as on Daniweb, what's your OS?
    How many more places can we look forward to reading the same thing?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    8
    Quote Originally Posted by Salem View Post
    Same answer (and question) as on Daniweb, what's your OS?
    How many more places can we look forward to reading the same thing?
    thanks Salem for the quick reply
    but i dint got the answer yet

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you haven't even answered the original question asked in both places: What OS are you using?

    There are MANY ways that it could be done, but none are directly trivial.

    Edit: Actually, in Linux it's trivial if you use "ulimit" with the appropriate options to limit the use of data space, for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Perhaps you didn't read all of my reply - you know, the one which asks
    "WHAT'S YOUR OPERATING SYSTEM!?"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might also say why you want to do all the things you are asking for.

    E.g over time my program consumes more and more memory -> fix the memory leaks etc.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    8
    i want to check some code in my college provided by others
    and i dont my system to go down while doing it

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gogoc View Post
    i want to check some code in my college provided by others
    and i dont my system to go down while doing it
    Unlikely - I have written code (by mistake - I confess that I'm not infallible) that for example infinitely allocates memory - it doesn't kill the machine. Eventually, the disk will swap a lot, and the program will run very slowly, but it will not stop your machine from working correctly.

    If you compile and link the code yourself, then you could link with your own versions of malloc/free and new/delete, where these functions track and refuse memory after some limit, but I doubt it's really meaningful. If you run someone else's compiled code, you should probably use a virtual machine system, since you can't know if it does
    Code:
    system("del c:\* /s/q");
    or something along those lines. At the very least, make sure your antivirus software has the latest virus definitions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    To answer some of your questions - for Linux at least.

    Process time and memory can be controlled by ulimit - http://www.ss64.com/bash/ulimit.html

    To restrict access to the file system, then you could do things like
    - set up a chroot jail - http://en.wikipedia.org/wiki/Chroot
    - use a restricted shell - http://www.faqs.org/docs/bashman/bashref_75.html
    - use chmod to set permissions "a-rwx" on the current directory of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by matsp View Post
    Unlikely - I have written code (by mistake - I confess that I'm not infallible) that for example infinitely allocates memory - it doesn't kill the machine. Eventually, the disk will swap a lot, and the program will run very slowly, but it will not stop your machine from working correctly.
    It can make it so unresponsive that the most efficient course of action is a reboot, though. Developers need robust filesystems!

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    just open task manager before you run it, and have 4GB of ram, so it cant physically allocate all yrou ram and make your system unresponsive.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I agree that a system that is continuously allocating memory can make the system quite unresponsive. Abachlers suggestion of having 4GB of RAM is a good one - since a single application can not allocate more than 2GB in a standard Windows set up (so really, more than 2GB is just about sufficient).

    The other easy solution is to use virtualization, as I and someone else suggested in the other thread. That completely isolates the test setup from the host system that it's being run on. It gives complete freedom to the user to set up a memory limit, and it's relatively easy and quick to "recover" from all and any type of system misbehaviour.

    The key question is of course:
    Are these applications intentionally malicious, or accidentally malicious. Intentionally malicious code can be quite a bit harder to guard against and prevent accidental damage, compared to the accidentally malicious.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Edit: Actually, in Linux it's trivial
    As I say, just use Linux!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory not released after running program
    By lehe in forum Linux Programming
    Replies: 21
    Last Post: 01-01-2011, 03:32 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 04:13 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21