2011-03-14 103 views
0

嗨 我有一个脚本,返回存储在mysql表中的所有记录。这个脚本在我的具有标准128MB内存限制的xampp服务器上工作得很好。如何调试内存不足问题

致命错误:在对存储器(分配524288)(试图分配4294967294个字节)/websites/LinuxPackage02/nw/cc/_u/nwcc-uk.org/public_html/code/Article.php上线37

但是,当它上传到web融合myserverworld平台,它崩溃。我将如何调试这样的问题?为什么要分配4GB内存?奇怪的是,我的开发服务器只有这么2GB怎么可能把它分配4.

public function getActivity($id,$language='ch') 
    { 

    $mysqli = new Database(); 
     $mysqli->connect(); 

    $mysqli->query("SET character_set_results=utf8"); 
    $sql = ($language == 'ch') ? 'SELECT id,title,text,date,author FROM article_chinese WHERE id=?': 'SELECT id,title,text,date,author FROM article_english WHERE id=?'; 
     if ($stmt = $mysqli->prepare("$sql")) { 
      $stmt->bind_param("i", $id); 
       /* execute query */ 
      $stmt->execute(); 

      /* bind result variables */ 
      $stmt->bind_result($id,$title,$text,$date,$author); 

      /* fetch value */ 
      $stmt->fetch(); 
      $results = array($id,$title,$text,$date,$author); 
      $stmt->close(); 
      $mysqli->close(); 
      return $results; 
    /* close statement */ 


     } 
    } 

回答

0

它试图精确地分配4GB内存(4294967294/1024/1024/1024 = 4),这一事实是可疑的。 Article.php中的第37行是什么,它试图从数据库中获取多少行?

+0

有问题的功能在上面。脚本崩溃尝试绑定结果。它只能从DB获得一行!这确实会造成非常严重的错误。任何关于这点的信息都会受到极大的关注。 – Ageis 2011-03-14 22:53:00

+0

我现在通过在绑定之前存储结果来解决此问题。有没有关于mysqli在这里失踪的基本知识?存储结果如此重要以至于分配的内存更少? – Ageis 2011-03-14 23:21:21

0

我一直运行到上一次同样的问题,我偶然发现了这个在php.net张贴在:

andrey at php dot net 07-Oct-2005 12:38

If you select LOBs use the following order of execution or you risk mysqli allocating more memory that actually used

1)prepare() 2)execute() 3)store_result() 4)bind_result()

If you skip 3) or exchange 3) and 4) then mysqli will allocate memory for the maximal length of the column which is 255 for tinyblob, 64k for blob(still ok), 16MByte for MEDIUMBLOB - quite a lot and 4G for LONGBLOB (good if you have so much memory). Queries which use this order a bit slower when there is a LOB but this is the price of not having memory exhaustion in seconds.

这就解释了为什么它试图分配这么多内存;我猜你的一个领域是一个blob?然后(如你自己写的)解决方案是在绑定结果之前使用store_result()