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
Printable View
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"?Quote:
Originally Posted by gogoc
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
You can try achieve this by implementing your own memory manager and overloading new operator.
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.Quote:
Originally Posted by gogoc
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.
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.Quote:
Originally Posted by Ajay Vijay
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.
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 ??
what is the scenario , I am curious to know it
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).Quote:
Originally Posted by gogoc
I sincerely hope not.Quote:
Originally Posted by gogoc
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 ..
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.
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.
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....
Ah. This is what virtual machines are for. Set one up on your system and run the code within it.