php中的销毁对象

php中的销毁对象,php,object,memory,garbage-collection,Php,Object,Memory,Garbage Collection,我正在用PHP执行一个程序,有时会出现以下错误。这是因为创建了很多对象而没有销毁它们还是其他原因 允许的内存大小为16777216字节,尝试分配19456字节 在PHP5中销毁对象的正确方法是什么 一些代码: App::import('Vendor','GoogleShipping',array('file'=>'googlecheckout'.DS.'library'.DS.'googleshipping.php')); App::import('Vendor','GoogleTax',

我正在用PHP执行一个程序,有时会出现以下错误。这是因为创建了很多对象而没有销毁它们还是其他原因

允许的内存大小为16777216字节,尝试分配19456字节 在PHP5中销毁对象的正确方法是什么

一些代码:

App::import('Vendor','GoogleShipping',array('file'=>'googlecheckout'.DS.'library'.DS.'googleshipping.php'));
App::import('Vendor','GoogleTax',array('file'=>'googlecheckout'.DS.'library'.DS.'googletax.php'));
class Cart{
    var $_itemname;
    var $_unit_price;
    public function Usecase($itemname,$unit_price,$url,$merchant_private_item_data)
    {
            $cart = new GoogleCart($this->_merchant_id, $this->_merchant_key, $this->_server_type,$this->_currency);
            $item_1 = new GoogleItem($this->_itemname,$this->_item_description,$this->_total_count,$this->_unit_price,$this->_merchant_private_item_data);
    }
}
用于销毁对对象的引用。当所有引用都消失时,对象将被销毁

您还可以利用该功能查看当前使用的内存量。尝试使用它找出内存瓶颈所在。

用于销毁对对象的引用。当所有引用都消失时,对象将被销毁


您还可以利用该功能查看当前使用的内存量。尝试使用它来找出内存瓶颈所在。

如果您这样做了,例如:

for($i=0;$i<=10000;$i++)
{
    $Object = new MyObject();
    //Blah
    unset($Object);
}
现在,您的内存不应达到峰值,您只需使用unset 20次:


通过这种方式,您还可以保存性能。

如果您这样做了,则效果会很好,例如:

for($i=0;$i<=10000;$i++)
{
    $Object = new MyObject();
    //Blah
    unset($Object);
}
现在,您的内存不应达到峰值,您只需使用unset 20次:


这样也可以节省性能。

如果没有看到代码,我们无法确切地说出导致此错误的原因。它可能创建了太多的对象,但也可能创建了太大的数组或其他数据

无论是哪种方式,不管您创建的是哪种类型的数据引发了错误,都可能是循环或递归函数失控,这是根本原因,在每次迭代中都会创建新数据,而不是故意创建足够的对象来溢出那么多内存


如果您确实需要创建足够的数据来填充这么多的内存,那么您可以在PHP.ini中修改最大内存余量,但这是不可能的。

如果没有看到代码,我们无法确切地说出导致此错误的原因。它可能创建了太多的对象,但也可能创建了太大的数组或其他数据

无论是哪种方式,不管您创建的是哪种类型的数据引发了错误,都可能是循环或递归函数失控,这是根本原因,在每次迭代中都会创建新数据,而不是故意创建足够的对象来溢出那么多内存


如果您确实需要创建足够的数据来填充这么多的内存,那么您可以修改PHP.ini中的最大内存余量,但这不太可能。

因为我在许多地方使用了大量的APP::import来导入cakephp中的供应商文件,它会消耗更多内存


我没有使用APP::import,而是使用了require_一次,错误没有发生。

因为我在许多地方使用APP::import的很多文件来导入cakephp中的供应商文件,它会消耗更多内存


我用require_代替了APP::import,但没有出现错误。

APP::import'Vendor','GoogleShipping',array'file'=>'googlecheckout'.DS.'library'.DS.'GoogleShipping.php';应用程序::导入'Vendor','GoogleTax',数组'file'=>'googlecheckout'.DS.'library'.DS.'GoogleTax.php';类Cart{var$\u itemname;var$\u unit\u price;公共函数用例$itemname,$unit\u price,$url,$merchant\u private\u item\u data{$cart=new GoogleCart$this->\u merchant\u id,$this->\u merchant\u key,$this->\u server\u type,$this->\u currency;$item\u 1=new GoogleItem$this->\u item name,$this->\u item\u description,$this->\u总数,$this->\u单价,$this->\u merchant\u private\u private\u item\u数据;}我导入的每个类也会创建一些对象。您可以使用编辑按钮编辑您的帖子,然后请将代码块中的代码附加到PostApp::import'Vendor','GoogleShipping',array'file'=>'googlecheckout'.DS.'library'.DS.'GoogleShipping.php';App::import'Vendor','GoogleTax',array'file'=>'googlecheckout'.DS.'library'.DS.'google.'php';类Cart{var$\u itemname;var$\u unit\u price;公共函数用例$itemname、$unit\u price、$url、$merchant\u private\u item\u data{$cart=new GoogleCart$this->\u merchant\u id,$this->\u merchant\u key,$this->\u server\u type,$this->\u currency;$item\u 1=new GoogleItem$this->\u item name,$this->\u item\u description,$this->\u总数,$this->\u单价,$this->\u merchant\u private\u private\u item\u数据;}我导入的每个类也会创建一些对象。您可以使用“编辑”按钮编辑您的帖子,然后请将代码块中的代码附加到帖子中您缺少$count上的增量您缺少$count上的增量