gpt4 book ai didi

php - 如何在运行较长的 PHP 脚本时清除内存?试过 unset()

转载 作者:IT老高 更新时间:2023-10-28 23:54:56 25 4
gpt4 key购买 nike

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

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

来自日志文件的错误信息: fatal error :允许的 851443712 字节内存已耗尽(已尝试分配 71 字节)

没有任何脚本包含在函数中。如果我在函数内创建主 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);

最佳答案

使用函数。
使用 $var = null; 而不是 unset($var);。取消设置只是杀死变量引用。


如上所述 comment :

When you are using unset, the memory will only be freed whenever garbage collector decides, but when you are setting a variable to a different value (null in this case), then you might get some memory freed of course with the cost of CPU.

关于php - 如何在运行较长的 PHP 脚本时清除内存?试过 unset(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10544779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com