简体   繁体   English

如何修复或优化代码以忽略PHP中的内存大小错误

[英]how to fix or optimize code to ignore memory size error in PHP

I wrote a image crawler with simple_html_dom library, I used this code to fetch all images in a web site ; 我用simple_html_dom库编写了一个图像爬虫,我使用这段代码来获取网站中的所有图像;

include 'simple_html_dom.php';
$img_array = array();
if (isset($_POST['url'])) {
    $url = $_POST['url'];
    $html = file_get_html($url);
    echo $html->getElementByTagName('title')->innertext();
    foreach ($html->find('a') as $a) {

        if (strpos($a->href, $url) !== FALSE) // only images from this site
        {
            //
            //    find_images($a->href);
            $imgs = file_get_html($a->href);
            foreach ($imgs->find('img') as $img) {
                if(!in_array($img->src, $img_array))
                {
                    echo '<img src="' .$img->src. '" class="thumb">';
                    $img_array[] = $img->src;
                }
            }
            echo '<hr>';
        }
    }
}

but whene i execute this code i get Fatal error: Allowed memory size of 209715200 bytes exhausted (tried to allocate 71 bytes) in /home/iphotosh/public_html/test/simple_html_dom.php on line 122 但是,当我执行此代码时,我得到Fatal error: Allowed memory size of 209715200 bytes exhausted (tried to allocate 71 bytes) in /home/iphotosh/public_html/test/simple_html_dom.php on line 122

test and demo : test.iphotoshop.ir 测试和演示: test.iphotoshop.ir

how to fix this error or how to opti,ize this code for fetch all images from a web site? 如何修复此错误或如何opti,ize此代码从网站获取所有图像?

您是否尝试使用ini_set()增加内存:

ini_set("memory_limit","256M");

You should do two things at the same time: Set your memory limit very high: 你应该同时做两件事:设置你的内存限制非常高:

in php.ini: 在php.ini中:

memory_limit = 512M

or/and in php file: 或/和在php文件中:

ini_set("memory_limit","512M");

At the same time you should delete big variables to free up some memory, usually via: 同时你应该删除大变量以释放一些内存,通常是通过:

unset($var);

By the way you can check the amount of used memory via 顺便说一句,您可以通过检查已用内存量

echo memory_get_usage();

I would try a demo run and check for memory usage in EACH line of your code, so you can see what happening here, and where to start your fixing 我会尝试一个演示运行并检查代码的每行中的内存使用情况,这样你就可以看到这里发生了什么,以及从哪里开始修复

Seems like you're trying to allocate way too much memory. 好像你试图分配太多的内存。 You could try increasing available memory in your php.ini (look for memory_limit= directive). 您可以尝试增加php.ini中的可用内存(查找memory_limit=指令)。 Yet, you could still exceed it if you are allocating A LOT. 然而,如果你要分配A LOT,你仍然可以超过它。 You can check dynamically what's available and how much is used: 您可以动态检查可用的内容和使用量:

function get_available_memory() {
    $ini_mem = ini_get('memory_limit');
    $m = substr($ini_mem, strlen($ini_mem) - 1;
    if($m == 'k' || $m == 'K') {
        $max_mem = 1024 * substr($ini_mem, 0, strlen($ini_mem) - 1);
    }
    elseif($m == 'm' || $m == 'M') {
        $max_mem = 1024 * 1024 * substr($ini_mem, 0, strlen($ini_mem) - 1);
    }
    elseif($m == 'g' || $m == 'M') {
        $max_mem = 1024 * 1024 * 1024 * substr($ini_mem, 0, strlen($ini_mem) - 1);
    }
    else {
        $max_mem = $ini_mem;
    }

    $used_mem = memory_get_usage(true);

    return $max_mem - $used_mem;
}

Now you can do 现在你可以做到

$available_memory = get_available_memory();

and, if not enough is available, do not try to allocate it and gracefully close your process. 并且,如果还不够,请不要尝试分配它并优雅地关闭您的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM