CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2008
    Posts
    11

    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
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to restrict memory usage of a c++ program

    Quote Originally Posted by gogoc
    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
    What do you mean by "restrict the total memory usage"?

    Please state exactly what you're trying to accomplish. A program loads, it has x amount of bytes reserved for free-store, stack, etc. I don't know what your question is really asking.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2007
    Location
    poland|wrocław
    Posts
    47

    Re: How to restrict memory usage of a c++ program

    You can try achieve this by implementing your own memory manager and overloading new operator.

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: How to restrict memory usage of a c++ program

    Quote Originally Posted by gogoc
    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
    I'm not a windows programmer so can't help with the windows OS, but the process of achieving this varies from OS to OS. Some real time OS's provide a means of setting a limit for the address space of an application. You'd setup some kind of linker definition file to structure the memory that is available to the program. You'd have to read the documentation for the OS and the compiler that builds the application in order to understand how to setup the project to achieve this. by being able to setup a limit for the application, the OS will start throwing exceptions once your program exceeds the limit.

  5. #5
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: How to restrict memory usage of a c++ program

    As macabre13 suggested, you need to implement global 'new' and 'delete' operators. They will allocate the memory (via malloc, for instance), and would keep track of number of bytes allocated. new-operator will add to global counter, and delete will decrease the global counter.

    But for all this to work you also need bookkeeping of each allocation, since when 'delete' is called you need to identify how much to reduce from global counter. vector or map would help.

    So, in new operator you can reject memory allocation if it is going to exceed the maximum.

    Overloading 'new' and 'delete' may cause issues with other libraries. So you may need to provide custom functions (like AllocateMem, DeleteMem). These can be global functions, or implemented in a (singleton) class. Multithreading should also be taken care of, if application is MT.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: How to restrict memory usage of a c++ program

    Quote Originally Posted by Ajay Vijay
    As macabre13 suggested, you need to implement global 'new' and 'delete' operators. They will allocate the memory (via malloc, for instance), and would keep track of number of bytes allocated. new-operator will add to global counter, and delete will decrease the global counter.
    You could do this but I am not sure if it is the only way nor am I sure if it is as simple as you suggest. There are other ways that memory can be allocated as well.

    If operator new and delete are overloaded, does that affect the STL classes, or would you also have to provide a custom allocator when constructing STL containers in order to ensure that they are using the custom operator new and delete? What about c-run time libraries that might allocate memory within the API calls? What about other user defined libraries that an application might link to? It seems like there are complex possibilities beyond just overloading operator new and delete.

    It'd be better if the project can be configured with some limitation so that if the heap grows beyond the limitation, exceptions will be thrown. This way you don't have to worry about code that might exist and allocate memory inside libraries that you are linking to as well as the memory allocated within STL objects. I'm not sure if the windows OS supports this type of limitation for an application but I do know that some real time OS's do.

  7. #7
    Join Date
    May 2008
    Posts
    11

    Re: How to restrict memory usage of a c++ program

    Thanks for the quick reply
    can i disable system provided malloc and calloc and other memory related function so that every one will have to use allocate memory by function provided by me ??

  8. #8
    Join Date
    Apr 2008
    Posts
    6

    Re: How to restrict memory usage of a c++ program

    what is the scenario , I am curious to know it

  9. #9
    Join Date
    Nov 2003
    Posts
    1,405

    Re: How to restrict memory usage of a c++ program

    Quote Originally Posted by gogoc
    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
    This must be an OS issue. I've never tried it but it must be possible to assign a certain amount of memory to a program (and set priority etcetera).

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: How to restrict memory usage of a c++ program

    Quote Originally Posted by gogoc
    Thanks for the quick reply
    can i disable system provided malloc and calloc and other memory related function so that every one will have to use allocate memory by function provided by me ??
    I sincerely hope not.

  11. #11
    Join Date
    May 2008
    Posts
    11

    Re: How to restrict memory usage of a c++ program

    the scenario is that i have to some programs on my system.
    the programs are quite simple, hardly 4-5 classes, i have to test it on my system but i dont want to mess up my system up because i dont know the code provided and c++ is really powerful ..

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to restrict memory usage of a c++ program

    Believe it or not, operating system developers *have* put some time into this. Protecting the system from memory overuse by a single process is the OS's responsibility, not yours.

  13. #13
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: How to restrict memory usage of a c++ program

    Please describe more detailed what you´re going to do. What kind of program are you working on when you (the programmer!) does not know how much memory it´s going to use. After all it´s you who´s deciding when and how new/malloc will be called.
    You started two other threads with similar intentions (How do I prevent this or that), so _please_ give us an overview of what you´re actually trying to do.
    - Guido

  14. #14
    Join Date
    May 2008
    Posts
    11

    Re: How to restrict memory usage of a c++ program

    i have to check some code which is provided by others.
    i have to check the outcome of the program so i want to protect my system as i dont want my system to go down when i am checking it....

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to restrict memory usage of a c++ program

    Ah. This is what virtual machines are for. Set one up on your system and run the code within it.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured