我在flie index.class.php
中创建了以下类:-
<?php
class index{
public function loadTitle() {
$title = "Welcome to My Website";
return $title;
}
}
?>
现在,我在我的index.php
文件中使用它,如下所示
<?php
require_once("index.class.php");
$obj = new index();
echo $obj->loadTitle();
?>
我的问题是,因为页面上会有很多文章和图片,我预计每天会有1500-2500名用户。
我还需要取消设置内存吗?我知道PHP有自己的垃圾收集器,但下面的URL让我害怕:-www.google.com/search?q=php+内存+泄漏+带+新
我在StackOverflow中看到了一些问题,说它确实会消耗内存,有些人建议使用unset
函数,但我不确定如何做到这一点。
这是我的尝试:
我只需要打吗
<?php
unset($obj); // At the end of the page, or where i will no more be using this object.
?>
或者我也必须设置NULL值。
<?php
$obj = NULL;
unset($obj);
?>
上面的代码可以释放内存吗?还是我也需要做其他事情?请建议并教我!
PHP具有自动垃圾收集功能。每次重新分配$obj
时,如果旧值不再可访问,则会回收其内存。因此,除非您将它们存储在某个地方,否则您只能引用一个对象,而不是所有您创建的对象。
首先,您不需要一直取消设置变量。我见过人们在PhP脚本的末尾取消设置所有变量,这是无用的。
你唯一想取消设置变量的时候是,如果你在PhP中有内存问题,你可以销毁脚本中其他地方不会使用的变量,但你还有很多事情要做(考虑时间或内存)。
你可能还想看看:
http://www.stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null
您应该研究一下PHP的垃圾收集器。如果你愿意,你甚至可以强制PHP收集垃圾:
<?php
gc_enable(); // Enable Garbage Collector
var_dump(gc_enabled()); // true
var_dump(gc_collect_cycles()); // # of elements cleaned up
gc_disable(); // Disable Garbage Collector
?>
来源:http://php.net/manual/en/features.gc.php