一段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; } }
完畢。
