Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php脚本超过垃圾收集?_Php_Memory Management_Sitemap - Fatal编程技术网

php脚本超过垃圾收集?

php脚本超过垃圾收集?,php,memory-management,sitemap,Php,Memory Management,Sitemap,我正在写一个脚本,为一个拥有700k产品的电子商务网站创建网站地图。电子商务软件包已经有了一个内置的站点地图生成器,但是对于大型产品数据库(带有指向Gzip 50k url文件链接的主数据库),它的格式不正确 在运行脚本时,我遇到了内存管理问题,似乎脚本的速度超过了php的垃圾收集能力。以下是我所拥有的: function buildSitemapFile() { // prep sitemap string $content = "sitemap header info";

我正在写一个脚本,为一个拥有700k产品的电子商务网站创建网站地图。电子商务软件包已经有了一个内置的站点地图生成器,但是对于大型产品数据库(带有指向Gzip 50k url文件链接的主数据库),它的格式不正确

在运行脚本时,我遇到了内存管理问题,似乎脚本的速度超过了php的垃圾收集能力。以下是我所拥有的:

function buildSitemapFile()
{
    // prep sitemap string
    $content = "sitemap header info";

    $max_entries_per_sitemap = 50000;
    $current_batch = 0;

    while (buildSitemapZip($current_batch, $content, $max_entries_per_sitemap))
    {
        $current_batch++;
    }

    // finish sitemap string, open/create file, write to it, close file

}

function buildSitemapZip($batch, &$main_content, $max_urls = 50000)
{
    $file_name = *create file path string*

    // add this file to the index file's content string
    $main_content .= *this sitemap info*

    // open and prep this batch's site map file
    $handle = fopen($file_name, "w");

    $content = *sitemap header info*

    require_once('path_to_Product.class.php');
    $product = new Product();

    $group_size = 1000;

    // on first time called, $group_start = 0 (0 * 50000), group_start loops till >= 50000 ((0+1) * 50000), and group_start increments by $group_size on each pass 
    for ($group_start = $batch * $max_urls; $group_start < ($batch + 1) * $max_urls; $group_start += $group_size)
    {
        // request the next group of products. the query ends with "order by id limit $group_start, $group_size"
        $prods = $product->query_db_for_products($group_start, $group_size);

        // loop product set adding urls to the content string
        foreach($prods as $key => $prod)
        {
            $content .= *get info from product object to fill out the sitemap content string*
        }       

        if (count($prods) != $group_size)
        {
            // end of list
            $return_val = *stop looping*
            break;
        }
    }

    // complete sitemap string, write it to file, close file
    // create and open gzip file, write to it, close it, erase temp sitemap file

    return $return_val;
}
函数buildSitemapFile()
{
//准备站点地图字符串
$content=“站点地图标题信息”;
$max_entries_per_sitemap=50000;
$current_batch=0;
while(buildSitemapZip($current\u batch,$content,$max\u entries\u per\u sitemap))
{
$current_batch++;
}
//完成站点地图字符串、打开/创建文件、写入、关闭文件
}
函数buildSitemapZip($batch,&$main\u content,$max\u url=50000)
{
$file_name=*创建文件路径字符串*
//将此文件添加到索引文件的内容字符串中
$main_content.=*此网站地图信息*
//打开并准备此批次的站点地图文件
$handle=fopen($file_name,“w”);
$content=*站点地图标题信息*
require_once('path_to_Product.class.php');
$product=新产品();
$group_size=1000;
//第一次调用时,$group_start=0(0*50000),group_start循环直到>=50000((0+1)*50000),group_start在每次传递时按$group_大小递增
对于($group_start=$batch*$max_URL;$group_start<($batch+1)*$max_URL;$group_start+=$group_size)
{
//请求下一组产品。查询以“订单id限制$group\U start,$group\U size”结束
$prods=$product->query\U db\U查询\U产品($group\U start,$group\U size);
//向内容字符串添加URL的循环产品集
foreach($prods as$key=>$prod)
{
$content.=*从产品对象获取信息以填写站点地图内容字符串*
}       
如果(计数($prods)!=$group\U大小)
{
//列表末尾
$return\u val=*停止循环*
打破
}
}
//完成站点地图字符串,将其写入文件,关闭文件
//创建并打开gzip文件,写入它,关闭它,删除临时站点地图文件
return$return\u val;
}
当我使用较小的$group_size值运行脚本时,这似乎不是什么问题,但它非常慢,并且开始减慢对数据库的访问(通过重复查询)

eccomerce包在其对象上没有_构造的_析构函数方法

我上一次运行测试时,使用的$group_大小为10k(应该是1k…),内存使用失控。我停止了脚本,但mem的使用量(根据top shell命令)一直在增加,几个小时后还没有发布


关于脚本中可能出现的导致问题的原因,您有什么想法吗?

我看不出您在哪里使用
$handle
。看起来您正在将整个站点地图放入一个字符串
$content
。我想对于这样大的站点地图,这个字符串会变得相当大。这个变量可能正在消耗我在那里看到的所有内存。@drew010,它是两个独立的函数。您将在第二个函数的末尾看到,其中有“complete string、write to file等”的注释。第二个函数写入的$content字符串将在每次调用该函数时创建。您的代码不可读。请正确缩进。@baiano它看起来太抽象了。我看不到发生了什么事。试着运行像XDebug这样的探查器,查看大部分内存使用发生在哪里,或者在整个过程中调用
memory\u get\u usage()
,手动尝试找出它何时开始使用过多内存。