Search code examples
phplaraveloptimizationmemory-managementlamp

PHP Memory exhausted


As I know the solution for this is.

ini_set('memory_limit','-1');

What if even this is not enough.

Problem is I am using a loop and creating and destroying the variables used in loop. But still I have not found the exact reason behind this. that memory utilization after every loop execution increases. my loop is going to run almost 2000 to 10000 times. So even 4gb ram is not going to enough.

As I observed using top commond that memory is using 50mb at the begining of loops, once loop goes on it increases size 10 to 15 mb after every iteration. So my code is not getting executed completely.

    ini_set('memory_limit', '-1');
             ini_set('xdebug.max_nesting_level', 1000);
             $ex_data = some data;
             $config = some data;
             $docConf = some data;
             $codeNameIndex = some data;
             $originalName = some data;
             CONST LIMIT = 3000;
             CONST START = 1000;

    //till here it is using 55 to 6o mb memory
            for ($i = self::START; $i < (self::START + self::LIMIT); $i++) {
                            $start_memory = memory_get_usage();
                            $object = new ImportProjectController();
                            $object->ex_data = $ex_data;
                            $object->config = $config;
                            $object->docConf = $docConf;
                            $StratProInsertDateTime = microtime(true);
                            try {
                                DB::connection()->getPdo()->beginTransaction();
                                $object->ex_data[$codeNameIndex[2]][$codeNameIndex[1]] = $originalName . '_' . $i;
                                $object->ex_data[$codeCodeIndex[2]][$codeCodeIndex[1]] = $originalCode . '_' . $i;
                                if (!$object->insert_project()) {
                                    throw new Exception('error while inserting project');
                                }
                                if (!$object->insert_documents()) {
                                    throw new Exception('error while inserting documents');
                                }
                                App::make('AccessController')->rebuildCache();
                                DB::connection()->getPdo()->commit();
                            } catch (Exception $ex) {
                                DB::connection()->getPdo()->rollBack();
                                echo $ex;
                            }
//it is increasing memory utilization every iteration.
                echo "Memory used for inserting a ".$i."th project :- ";
                echo memory_get_usage() - $start_memory.PHP_EOL;
                unset($object->ex_data);
                unset($object->config);
                unset($object->docConf);
                $object = null;
                echo "Memory utilization before inserting project :- ";
                echo memory_get_usage() - $start_memory.PHP_EOL;
            }
  • $object->insert_project()
  • $object->insert_documents()
  • App::make('AccessController')->rebuildCache()

Methods do some database inserts.

As I am unsetting the $object variable at the end of loop. but still it is not releasing the memory. And I am sure there is nothing that occupying the memory in above method.

Swap:        0k total,        0k used,        0k free,   241560k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                  
27671 ec2-user  20   0 1489m 1.1g 9908 R 66.0 30.4   8:15.00 php                                                                                      
 4307 mysql     20   0  852m 140m 5576 S 18.2  3.7  14:21.50 mysqld    

Above is the top commond output, as you can clearly see the memory utilization goes to 1.1gb. and it is increasing..

Plz let me know if need more description.


Solution

  • I got answer from my colleague for this problem.

    Laravel do a query logging, and all query keeps it in-memory, Thats why I was getting such issue. With the following code my script is running well with the use of only 250mb of memory. Hope this will help to others.

    DB::disableQueryLog();