Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)

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

完毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值