如何在Symfony 2.7中修复OutOfMemoryException

时间:2018-03-09 09:48:05

标签: php symfony-2.7

我使用真正的内存密集型函数从不同的SQL表和值生成csv导出文件。 当我选择一个大到的数据集时,我得到这个错误:

OutOfMemoryException in PDOStatement.php line 105:
Error: Out of memory (allocated 1636040704) (tried to allocate 12788 bytes)

我正在寻找一些想法来修复它并找到以下来源:

php.ini: memory_limit= '256M'

我做了这个修复,但它没有帮助我。

我也找到了以下提示:

config/monolog:buffer_size: 200

但它也没有帮助我..

进一步我试图禁用探查器......没有什么比这更好了!

有人有另一个想法吗?如何在不重构方法和SQL查询的情况下修复此错误。

以下函数代表了这个功能:

public function exportAction(Request $request){ 

//profiling data    
//$stopwatch = new Stopwatch();
//$stopwatch->start('Period');

if ($this->container->has('profiler'))
{
$this->container->get('profiler')->disable();
}


$data = $this->get('session')->get('dataE');
$data = unserialize($data);

$em = $this->getDoctrine()->getManager();

// restrict search result by user rights
if ($data['mnds'] == '1' || ($this->get('security.context')->isGranted('ROLE_EPO') && !$this->isAdmin())) {
    $data['registry'] = 1;
} elseif ($data['asp'] == '1' || ($this->get('security.context')->isGranted('ROLE_ASP') && !$this->isAdmin())) {
    $data['registry'] = 2;
}

// Load data structure
$repo = $em->getRepository('DataLiveBundle:DataZImportStructure');
$qb = $repo->getExportQuery($data);
$dStruct = $repo->getResults($qb);

$selectFields = array();
$tableNames = array();

foreach($dStruct as $ds) {
  $selectFields[] = $ds->getPrefix() . "." . $ds->getName();
  $tableNames[] = $ds->getSqlname();
}

//profiling data 
//$stopwatch->lap('Period');

// search data
$repo = $em->getRepository('DataLiveBundle:DataAPatient');
$dataTable = $repo->getDataExport($data, $selectFields);

// compile data
$rows = array();
$rows[0] = implode(';', str_replace(array(";", "\n"), '_', array_slice($tableNames, 0, -1)));

//$event = $stopwatch->stop('Period');
//$response = new Response("<h1>Hello World</h1>".$event->getDuration()." ms  ".$event->getMemory()." memory");

foreach ($dataTable as $row) {
  $cleanRow = array();
  foreach ($row as $entry) {
    if (is_numeric($entry)) {
      $cleanRow[] = $entry;
    } elseif (is_string($entry)) {
      $entry = str_replace(array(";", "\n"), '_',$entry);
      $cleanRow[] = preg_replace("/[\n\r]/","",$entry);
    } elseif (is_a($entry, 'DateTime')) {
      $cleanRow[] = $entry->format('d.m.Y');
    } else {
      $cleanRow[] = $entry;
    }
  }
  $rows[] = implode(";", array_slice($cleanRow, 0, -1));
}


$rawData = implode("\n", $rows);

// prepare csv 
$response = new Response($rawData);

//$response = new Response("<h1>Hello World</h1>".$event->getDuration()." ms  ".$event->getMemory()." memory");
$response = new Response($rawData);
//$response->headers->set('Content-Length', filesize($rawData));
$response->headers->set('Content-Type', 'text/csv');

return $response;

}

谢谢你的时间!

0 个答案:

没有答案