如何在运行长PHP脚本时清除内存?已尝试 unset()

2022-08-30 17:45:13

我的脚本将Excel文件导入产品数据库以更新数量新产品等。

我有内存问题,我已尝试将内存限制提高到最大(800MB +)。我尝试取消设置变量以释放循环之间的内存,但我仍然耗尽内存。我尝试将超时设置为无限,但这绝对是一个内存问题。

日志文件中的错误消息错误消息:致命错误:允许的内存大小851443712字节已耗尽(尝试分配 71 个字节)

函数中不包含任何脚本。如果我在函数内创建 main for 循环并重复调用该函数,这是否有助于垃圾回收和清理内存?任何帮助或指导将不胜感激。

导入脚本:

error_reporting( E_ALL & ~E_NOTICE );
ini_set('memory_limit', '812M');
set_time_limit(0);

/* Config Start */
define('BasePath', '/home/xxxxx/public_html');
define('CfgMagentoPath',                    BasePath);
define('CfgCategoryMapDBxls',                   BasePath."/xxxx/Shdddddd.xls");
define('CfgVenderDBxls',                    BasePath."/xxxx/xxxxxx.xls");
define('CfgReportEmail',                    "xxxxxx@gmail.com");
/* Config End */

require_once(CfgMagentoPath . '/app/Mage.php');
Mage::app(); 
//$app = Mage::app('default'); 
//Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
require_once(BasePath.'/xxxxx/xxxx/libs/mage.func-inc.php');
require_once(BasePath.'/xxxxx/xxxxx/libs/excel-read.class.php');

//Alert Arrays
$AAnotmapped        = array();
$AAnewproducts  = array();
$AApriceupdated = array();
$AAimgerror         = array();
$PriceErrors        = array();

$SkipCat = false;

//Create Mapped Cats - In Magento

$excel = new ExcelReader(CfgCategoryMapDBxls,"UTF-8");
$CM = $excel->getWorksheetData('Sheet1');
if(!$SkipCat){
    echo "========   Generating Catagory Maps   ===========\n\n";
    CatMap_Create($CM);
    echo "======== ============================== ===========\n\n";
}

//Start Item Read
$excel = new ExcelReader(CfgVenderDBxls,"UTF-8");
$IT = $excel->getWorksheetData('New_DATA');
$ITcnt = 0;
$ITtotal = count($IT);

foreach($IT as $ItemRow){
    $ITcnt++;

    $cSKU                   = $ItemRow['ITEM'];
    $cProductName   = Clean_Data($ItemRow['ALTSHORTDESC']);
    $cCatName           = Clean_Data($ItemRow['CATEGORY']);
    $cManuf                 = Clean_Data($ItemRow['MANUFACTURER']);
    $cShortDesc         = Clean_Data($ItemRow['SHORTDESC']);
    $cLongDesc          = Clean_Data($ItemRow['LONGDESC']);
    $cUPC                       = Prod_GetUPC($ItemRow['UPC'], $ItemRow['ALTUPC']);
    $cStockQty          = $ItemRow['QTY'];
    $cWeight                = Prod_GetWeight($ItemRow['WEIGHT'], $ItemRow['ALTWEIGHT']);
    $cPrice                 = Prod_FigurePrice($ItemRow['COST'], $ItemRow['MSRP'], $ItemRow['MAP']);
    $cCost                  = $ItemRow['COST'];


    //Locate Catagory Map Magento ID
    $mCatId = CatMap_Search($CM, $ItemRow['CATEGORY']);

    //Now Create Product
    if($mCatId > 0 && $cProductName != ""){

        echo date("m.d.y g:i a")."\t($ITcnt / $ITtotal) Working On: " . $cProductName . " - SKU: $cSKU\n";
        $ProdID = Prod_GetIDfromSKU($cSKU);


        if($ProdID > 0){
            if(Prod_Update($ProdID, $cCost, $cStockQty, $cWeight, $cUPC)){
                echo "Updated: $cProductName\n";
                $ITindex++;
            }
        }else{
            Prod_Create($cSKU, $cProductName, $cManuf, $cPrice, $cCost, $cWeight, $cShortDesc, $cLongDesc, $cStockQty, $cUPC, $mCatId);
            echo "Created: $cProductName to Catagory: $mCatId\n";
            echo "$cShortDesc\n\n";
            $ProdID = Prod_GetIDfromSKU($cSKU);
        }


        if($cPrice <= $cCost){
            array_push($PriceErrors, "[$cSKU] $cProductName > Cost: $cCost | Price: $cPrice");  
            echo "Price Lower than Cost : Auto Inactive : Cost: $cCost | Price: $cPrice\n";
        }   

        Prod_AddImg($ProdID, $cSKU);

    }


    unset($ItemRow, $ProdID, $cSKU, $cProductName, $cManuf, $cPrice, $cCost, $cWeight, $cShortDesc, $cLongDesc, $cStockQty, $cUPC, $mCatId);
    echo "\n";  

}


echo "======== Disabling 0 Product Catagories ===========\n\n";
Cat_Disable_Empty($CM);
echo "======== ============================== ===========\n\n";

unset($CM, $IT, $excel);

//array_push($AAnotmapped, 'Cat not Mapped');
//array_push($AApriceupdated, '### Price Updated');
//array_push($AAimgerror , 'Image Error');

Send_Status_Email();

Mage_Reindex();


echo date("m.d.y g:i a")."\tCompleted\n\n";

//print_r($AAnotmapped);

//print_r($AApriceupdated);

//print_r($AAimgerror);

答案 1

使用函数。
请代替 使用 。Unset 只是终止变量引用。$var = null;unset($var);


正如这篇评论中提到的:

当您使用unset时,只有当垃圾回收器决定时,内存才会被释放,但是当您将变量设置为不同的值(在本例中为null)时,您当然可能会以CPU的成本释放一些内存。


答案 2

即使使用函数,您也会期望垃圾回收器在函数返回时清理函数范围内的所有内容。这不是一个保证,如果你正在与内存使用作斗争,使用函数甚至可能对你不利。由于作用域的原因,php 必须创建作为参数传递的变量的副本,这只会增加内存使用量。您可以查看传递引用。

垃圾回收器仅在有可用的 CPU 周期时释放内存。通常在循环中,它不会有机会,因此它会在循环后尝试执行此操作,在这种情况下,它可能已经为时已晚。

但是,您可以通过调用 来强制垃圾收集器进行轮次。gc_collect_cycles

您也可以尝试使用memory_get_usage()


推荐