|
-
May 18th, 2008, 02:43 PM
#1
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
-
May 18th, 2008, 02:48 PM
#2
Re: How to restrict memory usage of a c++ program
 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
-
May 19th, 2008, 01:17 AM
#3
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.
-
May 19th, 2008, 11:05 AM
#4
Re: How to restrict memory usage of a c++ program
 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.
-
May 19th, 2008, 11:24 AM
#5
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.
-
May 19th, 2008, 11:37 AM
#6
Re: How to restrict memory usage of a c++ program
 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.
-
May 19th, 2008, 11:38 AM
#7
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 ??
-
May 19th, 2008, 11:39 AM
#8
Re: How to restrict memory usage of a c++ program
what is the scenario , I am curious to know it
-
May 19th, 2008, 11:41 AM
#9
Re: How to restrict memory usage of a c++ program
 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).
-
May 19th, 2008, 11:49 AM
#10
Re: How to restrict memory usage of a c++ program
 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.
-
May 19th, 2008, 12:34 PM
#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 ..
-
May 19th, 2008, 01:15 PM
#12
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.
-
May 20th, 2008, 03:31 AM
#13
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
-
May 20th, 2008, 02:15 PM
#14
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....
-
May 20th, 2008, 02:23 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|