Who moved my memory: php memory leakage, system cache consumption ?? I have consistently followed the title party style. I have not discussed the solution in depth .? Scenario: The image quality compressed by the online slice service (100) and the thumbnail quality (100) occupy a lot of space, resulting in 55 more files (160 GB of space) this problem was discovered. What needs to be solved now is to compress this part. who has moved my memory: php memory leakage and system cache consumption?
?
Continuing my consistent title party style, I have not discussed the solution in depth.
?
Scenario: The image quality compressed by the online slice service (100) and the thumbnail quality (100) occupy a lot of space, resulting in 55 more files (160 GB of space) this problem was discovered. What we need to solve now is to compress this part with a low-quality thumbnail to save space (of course, this is not necessary in this era of hard disk cabbage prices, I am not talking about space saving. I want to find out the memory consumption problem ). When I used a php script to generate a thumbnail again, I found that the memory consumption had been increasing through top, which caused the script to report that the memory was insufficient. Who moved my memory?
?
Process Code Version 1 (php ):
?
Set_time_limit (0); function thumbnailimage ($ img, $ width, $ height, $ savefile) {$ new_img = imagecreatetruecolor ($ width, $ height); imagedestroy ($ new_img );} // $ list: The name of the 0.55 million File, foreache ($ list as $ v) {$ img = imagecreatefromjpeg ($ v); thumbnailimage ($ img, 480,300, $ savepath ); imagedestroy ($ img );}?
?
When this script is processed for more than times, the script crashes due to insufficient memory, and then I changed php. the memory_limit in ini is changed to 5 GB, but the memory will be exhausted as the script is executed.
So I thought it was a php memory leak, and then I wanted to solve it with other solutions. I saw GraphicsMagick in Lao Wang's Technical Manual and wrote a script to process it, the result shows that the memory consumption on top is still increasing, and the system operation File (Write file) is cached, which consumes the memory,
Adjust the bash script, process an image, and manually release the memory (sync & echo 3>/proc/sys/vm/drop_caches). then, the memory consumption seen by top is normal.
This bash script is not processed as quickly as the gd library of php, and then changed to php for processing.
Verify the reason for memory consumption of php scripts:
Process Code Version 2 (php ):
?
Set_time_limit (0); function thumbnailimage ($ img, $ width, $ height, $ savefile) {$ new_img = imagecreatetruecolor ($ width, $ height );... imagedestroy ($ new_img);} // $ list: The 0.55 million file name foreache ($ list as $ v) {$ memory1 = memory_get_usage (); file_put_contents ('Memory ', 'memory1 :'. $ memory1. "\ n", FILE_APPEND); $ img = imagecreatefromjpeg ($ v); thumbnailimage ($ img, 480,300, $ savepath); imagedestroy ($ img ); $ memory1 = memory_get_usage (); file_put_contents ('Memory ', 'memory1 :'. $ memory1. "\ n", FILE_APPEND); system ('sync & echo 3>/proc/sys/vm/drop_caches ');}?
By using this version, the memory consumption will be normal. of course, the php process will also need to consume the memory, and the memory_limit of php. ini will be slightly changed.
From this point of view, the memory consumption is the consumption of system operating files, not php. due to my ignorance, PHP is incorrect at the beginning.
?
End: This example is just a brief description of the reason why I found the memory consumption.
Wudikua123 learning from.