如何调试内存不足的问题

Hi I have a script that returns all the records stored in a mysql table. This script works perfectly fine on my xampp server which has a standard 128MB memory limit.

Fatal error: Out of memory (allocated 524288) (tried to allocate 4294967294 bytes) in /websites/LinuxPackage02/nw/cc/_u/nwcc-uk.org/public_html/code/Article.php on line 37

But when upload it onto web fusions myserverworld platform, it crashes. How would I debug such an issue? Why is it trying to assign 4GB of memory? The weird thing is, My dev server only has 2GB so how could it assign 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 */


      }
    }

The fact it's trying to allocate exactly 4GB (4294967294/1024/1024/1024 =4) of memory is suspicious. What is around line 37 in Article.php, and how many rows is it trying to fetch from the DB?

I've been running into the same problem on occasion, and I stumbled upon this post over at 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.

This explains why it tries to allocate so much memory; I guess one of your fields is a blob? The solution then (as you wrote yourself) is to use store_result() before binding the result.