相关疑难解决方法(0)

克隆原型对象是否比从头创建对象提供了性能改进?

您可以在下方看到两个简化的摘要,其结果不会发生变化.

模式1,从头开始的对象:

foreach ($recipients as $recipient) {
    $message = new Message();
    $message->setBody("This is the body of the message.");
    $message->setRecipient($recipient);

    $transport->sendMessage($message);
    $persister->saveToDatabase($message); // Updated line
    unset($message);
}
Run Code Online (Sandbox Code Playgroud)

模式2,克隆原型对象:

$prototype = new Message();
$prototype->setBody("This is the body of the message.");

foreach ($recipients as $recipient) {
    $message = clone $prototype;
    $message->setRecipient($recipient);

    $transport->sendMessage($message);
    $persister->saveToDatabase($message); // Updated line
    unset($message);
}
unset($prototype);
Run Code Online (Sandbox Code Playgroud)

在内存使用,垃圾收集和/或CPU周期方面,对象克隆(模式2)是否提供了从头开始创建对象(模式1)的性能改进?还要考虑大量固定属性(在实例之间不会更改)和大量循环.


更新:我需要在每个循环中使用不同的对象实例.我添加saveToDatabase了对示例的调用以使其类似,例如,让它为消息提供ID.;)

php clone object

6
推荐指数
1
解决办法
1509
查看次数

PHP:对象数组和内存泄漏

从对象数组导出大量文件时,我遇到内存泄漏问题.简化代码如下所示:

class Test_Class
{
    private $a = null;

    public function __construct($a = null)
    {
        $this->a = $a;
    }

    public function __destruct()
    {
        unset($this->a);
    }
}

print 'Memory before: '.memory_get_usage(1).' <br>'; // 262 144 
$a = [];
for ($i=0; $i<600000; $i++)
    $a[] = new Test_Class($i);

print 'Memory after create: '.memory_get_usage(1).' <br>'; // 129 761 280 

for($i=0; $i < count($a); $i++)
    unset($a[$i]);

unset($a);

print 'Memory after: '.memory_get_usage(1).' <br>'; // 35 389 440 
Run Code Online (Sandbox Code Playgroud)

在下一次迭代中,内存仍然结束.知道如何释放占用的内存吗?

PS我尝试unset()/赋值null和gc_collect_cycles(),没有一个方法允许我释放对象数组占用的内存

php memory-leaks

5
推荐指数
1
解决办法
589
查看次数

PHP 在调用返回后是否会垃圾收集函数范围内的对象?

我知道一旦脚本结束,所有对象都会被销毁并返回内存。一旦函数结束且无论如何都无法访问的函数范围对象是否也会发生这种情况?

例如,我担心 PHPUnit 测试中的内存泄漏,其中我几乎为每个测试创建一个新对象。如果我运行足够的测试,这最终会溢出我的堆吗?

public function testMyFunction()
{
    // Arrange
    $myObject = new MyClass();

    // Act
    $return = $myObject->myFunction();

    // Assert
    $this->assertEquals(true, $return);

}
Run Code Online (Sandbox Code Playgroud)

我应该unset在“Absterge”部分中手动将它们设置为长时间运行的脚本吗?

public function testMyFunction()
{
    // Arrange
    $myObject = new MyClass();

    // Act
    $return = $myObject->myFunction();

    // Assert
    $this->assertEquals(true, $return);

    // Absterge
    unset($myObject);
}
Run Code Online (Sandbox Code Playgroud)

php memory phpunit memory-management

5
推荐指数
1
解决办法
562
查看次数

分配NULL和取消设置有什么区别?

看看下面的代码:

 <?php 
   $a = 20;
   $a= NULL;  //or unset($a)
   if(isset($a))
   {
      print("hi");
   }
   else
   {
       echo "not initiated";
   }
   if(isset($b))  //$b is a variable which is not initialized
   {
       print("hi");
   }
   else
   {
       echo "not initiated";
   }

?>
Run Code Online (Sandbox Code Playgroud)

当应用未设置时,我得到相同的结果:

那么,分配NULL和取消设置有什么区别?

php

0
推荐指数
2
解决办法
1059
查看次数

标签 统计

php ×4

clone ×1

memory ×1

memory-leaks ×1

memory-management ×1

object ×1

phpunit ×1