PHP访问数据库时超出内存

时间:2015-11-11 07:04:24

标签: php mysql database memory fatal-error

我正在使用PHP开发一个面向对象的网站,同时使用我刚学到的一些设计模式。 我有一个数据库的单例类和一些其他类,它们使用Database类的实例执行一些数据库操作。

类,

  1. 数据库(单身)
  2. 用户
  3. 节点
  4. 传感器
  5. <div class="panel-body">This is the body</div>
    

    其他类使用getConnection函数来获取单例实例

    class Database {
        //adaptor variable 
        private $db;
    
        //singleton instance
        private static $instance=NULL;
    
        private $config = array(
                                'host'      => 'localhost',
                                'username'  => 'XXXXXXX',
                                'password'  => '',
                                'dbname'    => 'XXX'
                                );
    
        private function __construct() {
            try
            {
                echo "using construct";
                //adaptor
                $this->db = new PDO('mysql:host=' . $this->config['host'] . ';dbname=' . $this->config['dbname'], $this->config['username'], $this->config['password']);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                die($e->getMessage());
                exit();
            }
        }
    
    
        public static function getConnection()
        {
            if(self::$instance==NULL)
            {
                self::$instance = new Database();
            }
            return self::$instance;
        }
    
        public function prepare($sql)
        {
            return $this->db->prepare($sql);
        }
    }
    

    所以这是每个班级的基本结构。

    我正在为数据库使用单例,因为我不希望在服务器中发生太多连接,这会耗尽我的记忆。

    为了检查单例是否正常工作,我在数据库类的构造函数中添加了一个echo。

    总而言之,这应该可以正常运作。并且它正常工作,直到我向User类添加了一些方法(能够删除节点,我在用户类中有550行,而且我想,它应该无关紧要)

    我得到的错误是这样的

      

    致命错误:允许的内存大小为134217728字节耗尽(尝试过   分配65488字节)   第18行的C:\ xampp \ htdocs \ WeatherCenter \ lib \ classes \ Database.php

    这是指向数据库类中的配置数组。 首先,我在“节点”课程中得到了这个。然后我添加了一些

      

    未设置()

    进入该类的方法然后它被移动到Database类。我不确定为什么会出现这个问题,我在Database类中没有任何重复的结构(但我在Node类中有)

    ini_set('memory_limit',' - 1');不是一个选项,因为我不想使用方法,我需要在这里调试错误。

    我会上传您需要的必要文件,

    提前致谢, Bhashithe

2 个答案:

答案 0 :(得分:1)

你需要找到这种巨大内存消耗的真正原因。您的错误消息中提到的行是完全误导的,因为您的大部分内存将被消耗在其他地方。

要查找代码的关键部分,您可以通过在适当的位置输出类似内容来调查内存使用情况:

echo "current: " . memory_get_usage(true) . " bytes max: " . memory_get_peak_usage(true) . " bytes";

请参阅http://php.net/manual/de/function.memory-get-usage.php

答案 1 :(得分:0)

There should be a checklist for debugging things like this.

Out of memory Error or this Allowed memory exceeded error is happening due to a infinite loop or just as the error states, you are out of dedicated memory for the task and the OS terminated your process.

What I did to debug this error is go through the program line by line and check whats happening in each and every line. You might have to take notes as well. As @maxhb pointed out, the error might not point to the exact location where the bug lies, but somewhere along the path where it went and lost its memory.

In my case there was two objects calling each other in their constructors which ultimately goes on forever creating objects inside the memory until you find that you don't have enough memory.

Lessons learned:

1 You should really stick with the class diagram and create the object oriented program.

2 you should trust what your error says, they are absolutely specific most of the time

3 recursively calling each other is never a good thing