Which Master moved the little brother my memory: PHP memory leaks, system cache consumption

Source: Internet
Author: User
Who Moved my memory: PHP memory leaks, system cache consumption?

?

Preface: Continue my usual title party style, say the example solution, no in-depth discussion.

?

Scenario: The image quality (100) of the compression of the line slice service, the thumbnail quality (100) takes up a lot of space, resulting in later 55 files (space 160G) to find this problem. What needs to be solved now is to save space for this part of compressing a low-quality thumbnail (of course, this is not necessary in the era of this hard drive cabbage price, and I'm not talking about saving space here to figure out memory consumption issues). I used the PHP script to regenerate the thumbnail, the top found that memory consumption has been increased caused by the foot of the report is not enough memory, who moved my memory?

?

Handling code version One (PHP):

?

Set_time_limit (0); function thumbnailimage ($img, $width, $height, $savefile) {    $new _img = Imagecreatetruecolor ($ width, $height);    Imagedestroy ($new _img);} $list: Is the file name of the 550,000 Foreache ($list as $v) {    $img = imagecreatefromjpeg ($v);    Thumbnailimage ($img, 480,300, $savepath);    Imagedestroy ($img);}
?

?

This script processing more than 5,000 times, because the memory is not enough to hang, and then I changed the php.ini inside the memory_limit changed to 5G, but as the script executes, the memory will be exhausted.

So I thought it was PHP memory leaks, and then want to use other solutions, in Lao Wang's technical manual see GraphicsMagick This tool, and then wrote a script to deal with, the results found top see the memory consumption has been increased, Then the person prompts that this should be the system operation file (write file) file is cached and consumes memory,

Adjust the bash script, manually release the memory after processing a picture (Sync && echo 3 >/proc/sys/vm/drop_caches), then top see the memory consumption is normal.

Since this bash script is not processed as fast as the PHP GD library, it is replaced with PHP processing.

To verify the PHP script memory consumption reasons:

Processing code version Two (PHP):

?

Set_time_limit (0); function thumbnailimage ($img, $width, $height, $savefile) {$new _img = Imagecreatetruecolor ($width, $ height); Imagedestroy ($new _img);} $list: Is the file name of the 550,000 Foreache ($list as $v) {    $memory 1=memory_get_usage ();    File_put_contents (' Memory ', ' memory1: '. $memory 1. " \ n ", file_append);        $img = Imagecreatefromjpeg ($v);    Thumbnailimage ($img, 480,300, $savepath);    Imagedestroy ($img);        $memory 1=memory_get_usage ();    File_put_contents (' Memory ', ' memory1: '. $memory 1. " \ n ", file_append);        System (' Sync && echo 3 >/proc/sys/vm/drop_caches ');}
?

This version of processing memory consumption is normal, of course, the PHP process also consumes memory, php.ini memory_limit slightly larger.

From here it seems that the consumption of memory is the system operation file consumption, not PHP, because my ignorance began to mistake PHP.

?

End: This example simply describes the reason why I found memory consumption.

1/F wudikua123 2012-05-09

Study.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.