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
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
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.
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.
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.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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 doesor something along those lines. At the very least, make sure your antivirus software has the latest virus definitions.Code:system("del c:\* /s/q");
--
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.
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.
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.
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.
As I say, just use Linux!Edit: Actually, in Linux it's trivial