phpexcel out of memory optimization

Recently when exporting excel occasional memory overflow error of our company's project, after the test found that when the amount of data in 5000 on this problem (the default php.ini memory is 128M)


Allowed memory size of 134217728 bytes exhausted (tried to allocate 43 bytes)


Memory overflow problem solving methods are as follows



Option 1 and Option 2 is considered a very simple and crude, but did not touch at all, that is why memory overflow, as a technical person we should ask in the end the Inquisitor


The main article under a brief description of how to troubleshoot my memory overflow problem and how to fix the

Steps

Business roughly logic

① query the database Remove all qualified data

② cyclic meet the conditions of data, data processing, and placed in a new array

③ use PHPEXCEL library to generate excel spreadsheet

Debug

With the memory_get_usage php function to view memory usage


断点1:8M 
业务逻辑①
断点2:47M
业务逻辑② 
断点3:82M
业务逻辑③
断点4:140M


From the graph we can draw the following conclusions


① business logic consumes memory: 39M = 47M - 8M

② business logic consumes memory: 35M = 82M - 47M

③ business logic consumes memory: 58M = 140M - 82M

Optimization of business logic ③

See description from the business logic, business logic is optimized ① can not afford to query the database system the inevitable overhead. I began to think it is to optimize the business logic ③

Business logic ③: The main is to call third-party library PHPEXCEL generate a table by the next google are mainly found to solve this problem through caching, PHPEXCEL of itself is taken into account this problem, you can check the article here  under the large amount of data phpExcel case memory overflow solve  


断点1:8M 
业务逻辑①
断点2:47M
业务逻辑② 
断点3:82M
业务逻辑③
断点4:117M


After this, memory usage

① business logic consumes memory: 39M = 47M - 8M

② business logic consumes memory: 35M = 82M - 47M

③ business logic consumes memory: 35M = 117M - 82M

② business logic optimization

② business logic simply traverse data processing, and to put to the new array. This will understand, due to the large array of memory overhead, it is no way to avoid. But I consider the question at noon, I noticed a little, useless for the variables we want and to terminate the (unset), so I found the business logic ① After traversing through the result set variables did not use it, you can log off directly away, so after optimizing memory usage as follows


断点1:8M 
业务逻辑①
断点2:47M
业务逻辑② 
断点3:31M
业务逻辑③
断点4:67M

 

After this, memory usage


① business logic consumes memory: 39M = 47M - 8M

② business logic consumes memory: -16M = 31M - 47M

③ business logic consumes memory: 36M = 67M - 31M

Achievement

Finally, optimized use only 67M memory, even if a default 128M limit we can be very good support ~ ~ Finally, in fact, we used the combination of three options

Game Over

After careful elimination optimization, in fact, we found that doing this kind of thing is quite inquisitive sense of accomplishment, and can be considered the bottom of the depth Barbara slowly, about php reference copy hope everyone can understand the next count and write

Reference material

Under phpExcel large amount of data out of memory to solve the case:  http://www.cnblogs.com/myx/archive/2013/05/20/phpExcel-setCache.html 

PHP development and expansion of core applications: http://www.cunmou.com/phpbook/ 




Original Address: phpexcel out of memory optimization
Tags: phpexcel    PHP    exhausted    Memory    Memory Overflow   

Intelligent Recommendation

Reproduced in: https: //my.oschina.net/54php/blog/757196

Guess you like

Origin blog.csdn.net/weixin_34311757/article/details/91518158