当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP内存溢出优化代码详解

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-13 13:34:15 浏览: 评论:0 

相信很多人做大批量数据导出和数据导入的时候,经常会遇到PHP内存溢出的问题,在解决了问题之后,总结了一些经验,整理成文章记录下。

优化点

1、优化SQL语句,避免慢查询,合理的建立索引,查询指定的字段,sql优化这块在此就不展开了。

2、查询的结果集为大对象时转数组处理,框架中一般有方法可以转,如Laravel中有toArray(),Yii2中有asArray()。

3、对于大数组进行数据切割处理,PHP函数有array_chunk()、array_slice()。

4、对于大型的字符串和对象,使用引用传递&。

5、用过的变量及时unset。

6、导出的文件格式由excel改为csv

7、ini_set(‘memory_limit',''),设置程序可以使用的内存(不建议这样做)。

内存管理

PHP的内存什么怎么管理的呢? 在学C语言时,开发者是需要手动管理内存。在PHP中,Zend引擎提供为了处理请求相关数据提供了一种特殊的内存管理器。请求相关数据是只需要服务单个请求,最迟会在请求结束时释放数据。

PHP内存溢出优化代码详解

防止内存泄漏并尽可能快地释放所有内存是内存管理的重要组成部分。因为安全原因,Zend引擎会释放所有上面提到的API锁分配的内存。

垃圾回收机制

简单说下:

PHP5.3之前,采用引用计数的方式管理。PHP中的变量存在zval的变量容器中,变量被引用的时,引用计数+1,变量引用计数为0时,PHP将在内存中销毁这个变量。但是在引用计数循环引用时,引用计数就不会消减为0,导致内存泄漏。

PHP5.3之后做了优化,并不是每次引用计数减少都进入回收周期,只有根缓冲区满额后才开始进行垃圾回收,这样可以解决循环引用的问题,也可以将总内存泄漏保持在一个阈值之下。

代码

由于使用phpexcel时经常会遇到内存溢出,下面分享一段生成csv文件的代码:

  1. <?php 
  2.  
  3. namespace api\service; 
  4.  
  5. class ExportService 
  6. { 
  7.  
  8.  public static $outPutFile = ''; 
  9.  
  10.  /** 
  11.  * 导出文件 
  12.  * @param string $fileName 
  13.  * @param $data 
  14.  * @param array $formFields 
  15.  * @return mixed 
  16.  */ 
  17.  public static function exportData($fileName = '', $data, $formFields = []) 
  18.  { 
  19.  $fileArr = []; 
  20.  $tmpPath = \Yii::$app->params['excelSavePath']; 
  21.  
  22.  foreach (array_chunk($data, 10000) as $key => $value) { 
  23.   self::$outPutFile = ''; 
  24.   $subject  = !emptyempty($fileName) ? $fileName : 'data_'; 
  25.   $subject  .= date('YmdHis'); 
  26.   if (emptyempty($value) || emptyempty($formFields)) { 
  27.   continue; 
  28.   } 
  29.  
  30.   self::$outPutFile = $tmpPath . $subject . $key . '.csv'; 
  31.   if (!file_exists(self::$outPutFile)) { 
  32.   touch(self::$outPutFile); 
  33.   } 
  34.   $index = array_keys($formFields); 
  35.   $header = array_values($formFields); 
  36.   self::outPut($header); 
  37.  
  38.   foreach ($value as $k => $v) { 
  39.   $tmpData = []; 
  40.   foreach ($index as $item) { 
  41.    $tmpData[] = isset($v[$item]) ? $v[$item] : ''; 
  42.   } 
  43.   self::outPut($tmpData); 
  44.   } 
  45.   $fileArr[] = self::$outPutFile; 
  46.  } 
  47.    
  48.  $zipFile = $tmpPath . $fileName . date('YmdHi') . '.zip'; 
  49.  $zipRes = self::zipFile($fileArr, $zipFile); 
  50.  return $zipRes; 
  51.  } 
  52.  
  53.  /** 
  54.  * 向文件写入数据 
  55.  * @param array $data 
  56.  */ 
  57.  public static function outPut($data = []) 
  58.  { 
  59.  if (is_array($data) && !emptyempty($data)) { 
  60.   $data = implode(',', $data); 
  61.   file_put_contents(self::$outPutFile, iconv("UTF-8", "GB2312//IGNORE", $data) . PHP_EOL, FILE_APPEND); 
  62.  } 
  63.  } 
  64.  
  65.  /** 
  66.  * 压缩文件 
  67.  * @param $sourceFile 
  68.  * @param $distFile 
  69.  * @return mixed 
  70.  */ 
  71.  public static function zipFile($sourceFile, $distFile) 
  72.  { 
  73.  $zip = new \ZipArchive(); 
  74.  if ($zip->open($distFile, \ZipArchive::CREATE) !== true) { 
  75.   return $sourceFile; 
  76.  } 
  77.  
  78.  $zip->open($distFile, \ZipArchive::CREATE); 
  79.  foreach ($sourceFile as $file) { 
  80.   $fileContent = file_get_contents($file); 
  81.   $file = iconv('utf-8', 'GBK', basename($file)); 
  82.   $zip->addFromString($file, $fileContent); 
  83.  } 
  84.  $zip->close(); 
  85.  return $distFile; 
  86.  } 
  87.    
  88.  /** 
  89.  * 下载文件 
  90.  * @param $filePath 
  91.  * @param $fileName 
  92.  */ 
  93.  public static function download($filePath, $fileName) 
  94.  { 
  95.  if (!file_exists($filePath . $fileName)) { 
  96.   header('HTTP/1.1 404 NOT FOUND'); 
  97.  } else { 
  98.   //以只读和二进制模式打开文件 
  99.   $file = fopen($filePath . $fileName, "rb"); 
  100.  
  101.   //告诉浏览器这是一个文件流格式的文件 
  102.   Header("Content-type: application/octet-stream"); 
  103.   //请求范围的度量单位 
  104.   Header("Accept-Ranges: bytes"); 
  105.   //Content-Length是指定包含于请求或响应中数据的字节长度 
  106.   Header("Accept-Length: " . filesize($filePath . $fileName)); 
  107.   //用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值 
  108.   Header("Content-Disposition: attachment; filename=" . $fileName); 
  109.  
  110.   //读取文件内容并直接输出到浏览器 
  111.   echo fread($file, filesize($filePath . $fileName)); 
  112.   fclose($file); 
  113.   exit(); 
  114.  } 
  115.  } 
  116. } 

调用处代码:

  1. $fileName = "库存导入模板"; 
  2. $stockRes = []; // 导出的数据 
  3. $formFields = [ 
  4.  'store_id' => '门店ID', 
  5.  'storeName' => '门店名称', 
  6.  'sku' => 'SKU编码', 
  7.  'name' => 'SKU名称', 
  8.  'stock' => '库存', 
  9.  'reason' => '原因' 
  10. ]; 
  11. $fileRes = ExportService::exportData($fileName, $stockRes, $formFields); 
  12. $tmpPath = \Yii::$app->params['excelSavePath']; // 文件路径 
  13. $fileName = str_replace($tmpPath, '', $fileRes); 
  14.  
  15. // 下载文件 
  16. ExportService::download($tmpPath, $fileName);

Tags: PHP内存溢出

分享到: