一段PHP程序执行报错:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)
去百度了一下,原来是php.ini中的内存分配的问题,默认php代码能够申请到的最大内存字节数就是134217728 bytes,如果代码执行的时候再需要更多的内存,就会报错了,于是就将php.ini文件中的配置改了一下:
代码如下:
memory_limit = 128M;//将128M改成了256M
但是之后一想,一个php脚本一次请求的内存空间就要超过128M,那不管你以后将memory_limit设置成多大,以后肯定有出问题的时候。
究其原因,是我在在编码时,仅仅对变量赋值,却从来没有 unset ($var) 过。导致了内存占用越来越多,所以以后一个变量不再使用之后,一定要记得unset掉它。
protected function execute(InputInterface $input, OutputInterface $output) { //ini_set('max_execution_time', '0'); $answerService = new AnswerService(); $taskService = new TaskService(); $taskStudentService = new TaskStudentService(); $statService = new StatService(); $stuService = new StudentService(); $unMarkedStudents = $taskService->findUnMarkedStudent(); if(count($unMarkedStudents)>0){ foreach($unMarkedStudents as $taskStuInfo) { $unMarkedQuestions = $taskService->findUnMarkedQuestion($taskStuInfo['taskId'],$taskStuInfo['studentId']); foreach($unMarkedQuestions as $v){ $answerService->submitUnmarks($taskStuInfo['id'],$taskStuInfo['taskId'],$taskStuInfo['studentId'],$v['questionId'],
$v['subIndexes']); } //修改taskStudent表的status状态 $taskStudentInfo = $taskStudentService->get($taskStuInfo['id']); $taskStudentInfo->updateTime = new DateTime(); $taskStudentInfo->status = 'MARKED'; $taskStudentService->update($taskStudentInfo,true); unset($taskStudentInfo); //生成该学生的趋势 $stuInfo = $stuService->get($taskStuInfo['studentId']); $statService->calTaskPolyFit($stuInfo, $taskStuInfo['subject']); unset($stuInfo); unset($unMarkedQuestions); } } else { return false; } }
完毕。