none
Win32 Job Object fails to run large amount of processes. RRS feed

  • Question

  • Hi,

    I am using Job objects on windows server 2008 R2 within a .NET 4.0 windows service application.

    The job created in the following way:

    _hJob = CreateJobObject(IntPtr.Zero, jobName);
                if (_hJob == IntPtr.Zero)
                    throw new JobManagementException("CreateJobObject failed");
    
                JobName = jobName;
                var basicInfo = new JOBOBJECT_BASIC_LIMIT_INFORMATION();
                basicInfo.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
    
                var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION();
                extendedInfo.BasicLimitInformation = basicInfo;
    
                int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
    
                if (!SetInformationJobObject(_hJob, JobObjectExtendedLimitInformation, ref extendedInfo, (uint)length))
                    throw new JobManagementException("SetInformationJobObject failed");
    
                _hIoCompletionPort = CreateIoCompletionPort(new IntPtr(-1), IntPtr.Zero, UIntPtr.Zero, 1);
                if (_hIoCompletionPort == IntPtr.Zero)
                {
                    throw new JobManagementException("CreateIoCompletionPort failed");
                }
    
                var ioPort = new JOBOBJECT_ASSOCIATE_COMPLETION_PORT();
                ioPort.CompletionKey = IntPtr.Zero;
                ioPort.CompletionPort = _hIoCompletionPort;
                length = Marshal.SizeOf(typeof(JOBOBJECT_ASSOCIATE_COMPLETION_PORT));
                if (!SetInformationJobObject(_hJob, JobObjectAssociateCompletionPortInformation, ref ioPort, (uint)length))
                    throw new JobManagementException("SetInformationJobObject failed");
    

    Each time I want to add a process to a job I use this :

    var si = new STARTUPINFO();
                var pi = new PROCESS_INFORMATION();
    
                bool success = CreateProcess(null, commandLine,
                    IntPtr.Zero, IntPtr.Zero, false,
                    ProcessCreationFlags.CREATE_SUSPENDED |          // wait until we assign it to this job
                    ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB | // don't join the parent's job (for example, when running under a debugger job)
                    ProcessCreationFlags.CREATE_NEW_CONSOLE,
                    IntPtr.Zero, null, ref si, out pi);
    
                if (!success)
                    throw new JobManagementException("CreateProcess failed");
    
                if (!AssignProcessToJobObject(_hJob, pi.hProcess))
                    throw new JobManagementException("AssignProcessToJobObject failed");
    
                if (-1 == ResumeThread(pi.hThread))
                    throw new JobManagementException("ResumeThread failed");

    Everything works great until at some point, usually when ~60 processes already running, the additional process I try to run crashes with OutOfMemoryException.

    I don't use any process amount limit, and no memory limits.

    The machine has 32GB of RAM and only 12GB are used.

    Am I missing something ?

    Thanks,

    Roman

    Monday, May 27, 2013 9:22 AM

Answers

All replies