duanleiming2014 2013-09-02 14:28
浏览 17
已采纳

如何处理由类的“新”方法生成的PHP中的内存泄漏

I have Created following class in flie index.class.php :-

<?php
class index{
        public function loadTitle() {
            $title = "Welcome to My Website";
            return $title;
        }
    }
?>

And now, I am using it like below in my index.php file

    <?php
        require_once("index.class.php");
        $obj = new index();
        echo $obj->loadTitle();
    ?>

My question because the Page will become heavy with lots of article and images, and I will expect 1500-2500 user every day in it.

Do I need to also unset memory, I know PHP has its own garbage collector, but following URL scares me out :- www.google.com/search?q=php+memory+leak+with+new

and I saw a few questions in StackOverflow saying it does consumes memory, and certain people have suggested to use the unset function, but I am not sure how to do it..

This is my attempt:

Do I need to only call

<?php
    unset($obj); // At the end of the page, or where i will no more be using this object.
?>

Or I have to set NULL value too.

<?php
    $obj = NULL;
    unset($obj);
?>

Is the above code fine for releasing memory, or do I need to do something else too? Please suggest and teach me!

  • 写回答

3条回答 默认 最新

  • dongti7838 2013-09-02 14:35
    关注

    Firstly, you don't need to unset your variables all the time. I've seen people unset ALL their variables at the end of their PhP script, which is useless.

    The only time where you'd want to unset variables is if you've memory problems in PhP and you can destroy variables you won't use anywhere else in your script, but you still have a lot of things to do (considering time or memory).

    You might also want to give a look at :

    http://www.stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用