1 //总体来看是和内存设置有关的 2 #if defined(WIN32) && defined(MAX_MEMORY_GB) 3 if( MAX_MEMORY_GB>0 ) 4 { 5 //SIZE_T是ULONG_PTR类型又是unsigned __int64类型取值范围为2到2的64次方,貌似和64为操作系统支持的理论内存值有关系 6 SIZE_T peakMemory = 1; 7 peakMemory <<= 30;//peakMemory等于peakMemory(1)乘以2的30次方,2的30次方就是1GB,也就是说peakMemory是1GB 8 peakMemory *= MAX_MEMORY_GB; 9 //%.2f宽度无限制,小数点后保留两位 10 printf( "Limiting memory usage to %.2f GB\n" , float( peakMemory>>30 ) ); 11 //创建了一个工作对象(lpJobAttributes,lpName)安全属性+工作对象名称 12 HANDLE h = CreateJobObject( NULL , NULL ); 13 AssignProcessToJobObject( h , GetCurrentProcess() ); 14 15 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; 16 jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_JOB_MEMORY; 17 jeli.JobMemoryLimit = peakMemory; 18 if( !SetInformationJobObject( h , JobObjectExtendedLimitInformation , &jeli , sizeof( jeli ) ) ) 19 fprintf( stderr , "Failed to set memory limit\n" ); 20 } 21 #endif // defined(WIN32) && defined(MAX_MEMORY_GB)
时间: 2024-10-10 12:32:44