How do I start a process with limited memory?

How do I create and run a process (e.g. run an .exe file) with limited RAM using C ++ and win32 API?

What error code will be returned if the process goes out of bounds?

+2


source to share


5 answers


Job objects are the right way to go.

As for the error code, there isn't really one. You create a process (with CreateProcess) and a job (with CreateJobObject) and then associate the process with a job object (with AssignProcessToJobObject).



The parent process will not receive an error message if the child allocates more than the allowed amount of memory. In fact, the limit will apply even if the parent process exits. If the child tries to allocate more than the allowed amount of memory, the allocation will simply fail.

+2


source


You can use CreateProcess () to create a process.

Once you have done that, you can use SetProcessWorkingSetSize () to try and control how much physical memory it is using, but this is more of a really strong suggestion for the VMM than some actual edict that will force malloc () and new to start failing ...



It is impossible to say that "this process will take 4MB of memory and after that all allocations will fail." I mean you are going to link to win32 dlls and you have no idea what memory usage they require. If you want your application to take up a certain amount of memory, don't allocate more than that. And don't do what allocates memory.

Your question regarding the error code doesn't make any sense.

+2


source


NT Job Objects (SetInformationJobObject and JOBOBJECT_BASIC_LIMIT_INFORMATION

)

+2


source


According to my information, there is no such possibility in windows. It would be very useful to have other things at least for testing.

You have this in java as the JVM only uses a predefined amount of memory, but there is not a function there, but a problem; -)

+1


source


If you start a process, you lose control of that process. It's just that the operating system can control its behavior (memory, i.e., T.E.), But even so I can't imagine how this can be achieved, as jeffamaphone states, any limitation is a suggestion at best. not an instruction. The process can call external static libraries, COM instances, etc., so I have no idea how this limit can be checked / enforced.

+1


source







All Articles