public void UpdateMemoryLimit(uint? minPrcWorkingSet, uint? maxPrcWorkingSet, uint? maxJobVirtualMemory) { var flags = JOBOBJECT_BASIC_LIMIT_FLAGS.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; var basicLimits = new JOBOBJECT_BASIC_LIMIT_INFORMATION(); var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = basicLimits, }; if (minPrcWorkingSet.HasValue && maxPrcWorkingSet.HasValue) { flags |= JOBOBJECT_BASIC_LIMIT_FLAGS.JOB_OBJECT_LIMIT_WORKINGSET; basicLimits.MinimumWorkingSetSize = (UIntPtr)minPrcWorkingSet.Value; basicLimits.MaximumWorkingSetSize = (UIntPtr)maxPrcWorkingSet.Value; } if(maxJobVirtualMemory.HasValue) { flags |= JOBOBJECT_BASIC_LIMIT_FLAGS.JOB_OBJECT_LIMIT_JOB_MEMORY; extendedInfo.JobMemoryLimit = (UIntPtr)maxJobVirtualMemory.Value; } basicLimits.LimitFlags = (uint)flags; int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) { Marshal.FreeHGlobal(extendedInfoPtr); throw new Exception(string.Format("Unable to set extended information. Error: {0}", Marshal.GetLastWin32Error())); } Marshal.FreeHGlobal(extendedInfoPtr); }
public void UpdateMemoryLimit(uint?minPrcWorkingSet, uint?maxPrcWorkingSet, uint?maxJobVirtualMemory) { var flags = JOBOBJECT_BASIC_LIMIT_FLAGS.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; var basicLimits = new JOBOBJECT_BASIC_LIMIT_INFORMATION(); var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = basicLimits, }; if (minPrcWorkingSet.HasValue && maxPrcWorkingSet.HasValue) { flags |= JOBOBJECT_BASIC_LIMIT_FLAGS.JOB_OBJECT_LIMIT_WORKINGSET; basicLimits.MinimumWorkingSetSize = (UIntPtr)minPrcWorkingSet.Value; basicLimits.MaximumWorkingSetSize = (UIntPtr)maxPrcWorkingSet.Value; } if (maxJobVirtualMemory.HasValue) { flags |= JOBOBJECT_BASIC_LIMIT_FLAGS.JOB_OBJECT_LIMIT_JOB_MEMORY; extendedInfo.JobMemoryLimit = (UIntPtr)maxJobVirtualMemory.Value; } basicLimits.LimitFlags = (uint)flags; int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length)) { Marshal.FreeHGlobal(extendedInfoPtr); throw new Exception(string.Format("Unable to set extended information. Error: {0}", Marshal.GetLastWin32Error())); } Marshal.FreeHGlobal(extendedInfoPtr); }