相信很多人做大批量數據導出和數據導入的時候,經常會遇到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引擎提供為了處理請求相關數據提供了一種特殊的內存管理器。請求相關數據是只需要服務單個請求,最遲會在請求結束時釋放數據。
防止內存泄漏并盡可能快地釋放所有內存是內存管理的重要組成部分。因為安全原因,Zend引擎會釋放所有上面提到的API鎖分配的內存。
垃圾回收機制
簡單說下:
PHP5.3之前,采用引用計數的方式管理。PHP中的變量存在zval的變量容器中,變量被引用的時,引用計數+1,變量引用計數為0時,PHP將在內存中銷毀這個變量。但是在引用計數循環引用時,引用計數就不會消減為0,導致內存泄漏。
PHP5.3之后做了優化,并不是每次引用計數減少都進入回收周期,只有根緩沖區滿額后才開始進行垃圾回收,這樣可以解決循環引用的問題,也可以將總內存泄漏保持在一個閾值之下。
代碼
由于使用phpexcel時經常會遇到內存溢出,下面分享一段生成csv文件的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<?php namespace api\service; class ExportService { public static $outPutFile = '' ; /** * 導出文件 * @param string $fileName * @param $data * @param array $formFields * @return mixed */ public static function exportData( $fileName = '' , $data , $formFields = []) { $fileArr = []; $tmpPath = \Yii:: $app ->params[ 'excelSavePath' ]; foreach ( array_chunk ( $data , 10000) as $key => $value ) { self:: $outPutFile = '' ; $subject = ! empty ( $fileName ) ? $fileName : 'data_' ; $subject .= date ( 'YmdHis' ); if ( empty ( $value ) || empty ( $formFields )) { continue ; } self:: $outPutFile = $tmpPath . $subject . $key . '.csv' ; if (! file_exists (self:: $outPutFile )) { touch(self:: $outPutFile ); } $index = array_keys ( $formFields ); $header = array_values ( $formFields ); self::outPut( $header ); foreach ( $value as $k => $v ) { $tmpData = []; foreach ( $index as $item ) { $tmpData [] = isset( $v [ $item ]) ? $v [ $item ] : '' ; } self::outPut( $tmpData ); } $fileArr [] = self:: $outPutFile ; } $zipFile = $tmpPath . $fileName . date ( 'YmdHi' ) . '.zip' ; $zipRes = self::zipFile( $fileArr , $zipFile ); return $zipRes ; } /** * 向文件寫入數據 * @param array $data */ public static function outPut( $data = []) { if ( is_array ( $data ) && ! empty ( $data )) { $data = implode( ',' , $data ); file_put_contents (self:: $outPutFile , iconv( "UTF-8" , "GB2312//IGNORE" , $data ) . PHP_EOL, FILE_APPEND); } } /** * 壓縮文件 * @param $sourceFile * @param $distFile * @return mixed */ public static function zipFile( $sourceFile , $distFile ) { $zip = new \ZipArchive(); if ( $zip ->open( $distFile , \ZipArchive::CREATE) !== true) { return $sourceFile ; } $zip ->open( $distFile , \ZipArchive::CREATE); foreach ( $sourceFile as $file ) { $fileContent = file_get_contents ( $file ); $file = iconv( 'utf-8' , 'GBK' , basename ( $file )); $zip ->addFromString( $file , $fileContent ); } $zip ->close(); return $distFile ; } /** * 下載文件 * @param $filePath * @param $fileName */ public static function download( $filePath , $fileName ) { if (! file_exists ( $filePath . $fileName )) { header( 'HTTP/1.1 404 NOT FOUND' ); } else { //以只讀和二進制模式打開文件 $file = fopen ( $filePath . $fileName , "rb" ); //告訴瀏覽器這是一個文件流格式的文件 Header( "Content-type: application/octet-stream" ); //請求范圍的度量單位 Header( "Accept-Ranges: bytes" ); //Content-Length是指定包含于請求或響應中數據的字節長度 Header( "Accept-Length: " . filesize ( $filePath . $fileName )); //用來告訴瀏覽器,文件是可以當做附件被下載,下載后的文件名稱為$file_name該變量的值 Header( "Content-Disposition: attachment; filename=" . $fileName ); //讀取文件內容并直接輸出到瀏覽器 echo fread ( $file , filesize ( $filePath . $fileName )); fclose( $file ); exit (); } } } |
調用處代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$fileName = "庫存導入模板" ; $stockRes = []; // 導出的數據 $formFields = [ 'store_id' => '門店ID' , 'storeName' => '門店名稱' , 'sku' => 'SKU編碼' , 'name' => 'SKU名稱' , 'stock' => '庫存' , 'reason' => '原因' ]; $fileRes = ExportService::exportData( $fileName , $stockRes , $formFields ); $tmpPath = \Yii:: $app ->params[ 'excelSavePath' ]; // 文件路徑 $fileName = str_replace ( $tmpPath , '' , $fileRes ); // 下載文件 ExportService::download( $tmpPath , $fileName ); |
到此這篇關于PHP內存溢出優化代碼詳解的文章就介紹到這了,更多相關PHP內存溢出優化內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/liuxingjiaoyuC/article/details/114022094