销毁php中的对象

发布于 2024-10-03 20:33:50 字数 833 浏览 2 评论 0原文

我正在 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);
    }
}

I am executing a program in PHP and getting the below error sometimes.Is this due to creating lot's of objects and not destroying them or any other reason?

Allowed memory size of 16777216 bytes exhausted (tried to allocate 19456 bytes)
What is the correct method to destroy an object in PHP5.

Some code:

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追风人 2024-10-10 20:33:50

由于我在很多地方使用大量 APP::import 来导入 cakephp 中的供应商文件,因此会消耗更多内存。

我没有使用 APP::import,而是使用 require_once() 并且没有发生错误。

As i was using lot's of APP::import in many places to import vendor files in cakephp it consumes more memory.

Instead of using APP::import i used require_once() and the error did not occurs.

无声情话 2024-10-10 20:33:50

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

您还可以使用 memory_get_usage 函数查看当前使用了多少内存。尝试用它来找出你的内存瓶颈在哪里。

Use unset to destroy the reference to your object. When all references are gone the object will get destroyed.

You can also make use of the memory_get_usage function to see ow much memory you are using at the moment. Try using it to find out where your memory bottlenecks are.

满身野味 2024-10-10 20:33:50

好吧,如果你这样做了,例如:

for($i=0;$i<=10000;$i++)
{
    $Object = new MyObject();
    //Blah
    unset($Object);
}

你不仅创建一个对象 10K 次,还使用 ​​unset 10K 次,这会降低应用程序的速度,

如果迭代如下,你最好将它们扰乱 X 次:

$array = array();
$count = 0;
for($i=0;$i<=10000;$i++)
{
    $array[$i] = new MyObject();
    //Blah
    $i++;
    if($count == 500)
    {
        unset($array,$count);
        $array = $array();
        $count = 0;
    }
}

现在你的内存不应该达到峰值,并且你只使用 unset 20 次:)

这样你的保存性能也是如此。

Well if you done this for example:

for($i=0;$i<=10000;$i++)
{
    $Object = new MyObject();
    //Blah
    unset($Object);
}

your not only creating an object 10K times your also using unset 10K times which would decrease the speed of your application

your best of unsettling them over X amount if iterations like so:

$array = array();
$count = 0;
for($i=0;$i<=10000;$i++)
{
    $array[$i] = new MyObject();
    //Blah
    $i++;
    if($count == 500)
    {
        unset($array,$count);
        $array = $array();
        $count = 0;
    }
}

Now your memory should not reach peak, and your only using unset 20 times :)

This way your saving performances as well.

本宫微胖 2024-10-10 20:33:50

如果不查看代码,我们无法准确判断导致此错误的原因。它可能创建了太多的对象,但也可能很容易创建太大的数组或其他数据。

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

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

Without seeing the code, we can't tell exactly what is causing this error. It could be creating too many objects, but it could just as easily be creating too big an array or other data.

Either way regardless of what type of data you're creating that is throwing the error, it's likely to be a loop or recursive function running out of control that's the underlying cause, creating new data on every iteration, rather than you deliberately creating enough objects to overflow that much memory.

If you really do need to create enough data to fill that much memory then you can modify the maximum memory allowance in PHP.ini, but it's unlikely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文