I want to start application using Process with some memory limit. I use AssignProcessToJobObject method from kernel32.dll to do it. I try to start application which consumes 10 MB of memory with 1 MB memory limit. But after start I see a dialog window with startup application error. How can I handle it?
-
11MB memory limit? You expect a c# application to consume less than 1MB? Of course you'll get an error. – Camilo Terevinto Dec 13 '15 at 17:06
-
It's only a sample. I chose memory limit to be sure that it would be an error. – timbset Dec 13 '15 at 17:12
-
@timbset and what do you want to do in response to the error? – usr Dec 13 '15 at 17:15
-
@usr I don't think he understands that if a process needs a certain amount of memory, you can't give it less and expect it to work. timbset, are you purposely trying to handle a purposely thrown error? That makes no sense to me... – Camilo Terevinto Dec 13 '15 at 17:17
-
I want only to write a message to console that I can't start an application or I started it but it is killed after memory limit. But I don't want to see a dialog windows blocking my main application. – timbset Dec 13 '15 at 17:18
-
@cFrozenDeath, started application would be able to use 1 GB of memory and I'm going to set 100 MB memory limit. Is it a big difference between these situations? In other words, I set less memory limit that application really needs. And I'm looking for some opportunities to know that my application is not executed correct without dialoag windows. – timbset Dec 13 '15 at 17:22
-
If the application uses 1GB of RAM, what makes you think it "needs" less? How would you know that? Did you program that application in such a bad way that it uses 1GB but in fact only needs 100MB? – Camilo Terevinto Dec 13 '15 at 18:38
-
@cFrozenDeath: Maybe he has a multiuser system, and doesn't want one process using up all the RAM in the computer, because it would interfere with other users. Just because a task "needs" that much memory doesn't mean you have to decide to pay it, you can decide you don't need to run that task after all. (Also, OP's typos and poor grammar aren't helping, but give him a break, English is probably not his first language) – Ben Voigt Dec 13 '15 at 18:46
-
@cFrozenDeath, I want to execute applications developed by other developers. So I want to check these applications' memory usage and say "I told you to use 100 MB but you use 1 GB. It's wrong" – timbset Dec 30 '15 at 08:21
1 Answers
The MSDN article Job Limits and Notifications decribes how to get notification of limits being exceeded.
Note that this notification is instead of killing the process, which you'll have to do yourself. Because the process is killed by your supervisor program, and not by a resource limit, Windows then won't pop up the dialog box you're trying to avoid.
If you do use the "hard" limits that cause immediate process termination, you can still detect the resource limit was triggered.
The job object records basic accounting information for all its associated processes, including those that have terminated. To retrieve this accounting information, use the
QueryInformationJobObject
function.
But then you won't avoid the OS-provided dialog box that the limit killed the process. Anyway, this accounting doesn't seem to apply to memory limits. So you should use the notification scheme instead.
Take a look at the JOBOBJECT_LIMIT_VIOLATION_INFORMATION
structure.

- 277,958
- 43
- 419
- 720
-
I don't really understand how this is related to the op's question. – Camilo Terevinto Dec 13 '15 at 18:42
-
@cFrozenDeath: OP's child process is dying due to exceeding a resource limit set in a Job Object. I'm explaining how to configure that Job Object to generate notifications when the limit is exceeded. – Ben Voigt Dec 13 '15 at 18:44
-
I understand that, but I don't understand the point in all this. If you know the task will use 1GB of RAM and will crash if you give it less, what's the point trying to run the Task that way? After all, you already know it's gonna crash. – Camilo Terevinto Dec 13 '15 at 19:13
-
@cFrozenDeath: to test the limit setting code you plan to use with future tasks with unknown requirements? – Ben Voigt Dec 13 '15 at 19:15
-