2012-10-02 20 views
0

請問能否幫我找到造成這個進程達到500MB內存使用情況的原因。 它基本上是一個html頁面下載器。 儘管這個過程是穩定的(並且沒有超過這個限制),但它意味着在低性能機器上使用,我並不滿意。 mysql表'Sites'的大小爲170MB。 遵循腳本代碼。 在此先感謝。減少php守護進程的內存使用情況

function start() { 
try { 
    global $log; 
    $db = getConnection(); 
    Zend_Db_Table::setDefaultAdapter($db); 
    $log->logInfo("logger start"); 
    while (1) { 
     $sitesTable = new Zend_Db_Table('Sites'); 
     $rowset = $sitesTable->fetchAll(); 
     foreach ($rowset as $row) { 
      if (time() >= (strtotime($row->lastUpdate) + $row->pollingHours * 60 * 60)) { 
       db_updateHtml($row); 
      } 
     } 
    } 
} catch (Exception $e) { 
    global $log; 
    $log->logError($e->getMessage()); 
} 
} 

    function db_updateHtml($siteRecord) { 
     try { 
      if ($siteRecord instanceof Zend_Db_Table_Row) { 
       $rowwithConnection = $siteRecord; 
       $url = $siteRecord->url; 

       $idSite = $siteRecord->idSite; 
       $crawler = new Crawler(); 
       $sitesTable = new Zend_Db_Table('Sites'); 
       //$rowwithConnection = $sitesTable->fetchRow(
        //  $sitesTable->select()->where('idSite = ?', $idSite)); 
       $newHtml = HtmlDbEncode($crawler->get_web_page($url)); 


       if (strlen($newHtml) < 10) { 
        global $log; 
        $log->logError("Download failed for: url: $url \t idsite: $idSite "); 
       } 
       if ($rowwithConnection->isChecked != 0) { 
        $rowwithConnection->oldHtml = $rowwithConnection->newHtml; 
        $rowwithConnection->isChecked = 0; 
       } 
       $rowwithConnection->newHtml = $crawler->get_web_page($url); 
       $rowwithConnection->lastUpdate = date("Y-m-d H:i:s"); 
       //$rowwithConnection->diffHtml = getDiff($rowwithConnection->oldHtml, $rowwithConnection->newHtml, false, $rowwithConnection->minLengthChange); 
       $rowwithConnection->diffHtml = getDiffFromRecord($rowwithConnection, false, $rowwithConnection->minLengthChange); 
       /* if (strlen($rowwithConnection->diffHtml) > 30) { 
        $rowwithConnection->lastChanged = $rowwithConnection->lastUpdate; 
        } */ 
       $rowwithConnection->save(); 
      } else { 
       $log->logCrit("siteRecord is uninitialized"); 
      } 
     } catch (Exception $e) { 
      global $log; 
      $log->logError($e->getMessage()); 
     } 
    } 

    function getDiffFromRecord($row, $force = false, $minLengthChange = 100) { 
     if ($row instanceof Zend_Db_Table_Row) { 
      require_once '/var/www/diff/library/finediff.php'; 
      include_once '/var/www/diff/library/Text/Diff.php'; 
      $diff = new AndreaDiff(); 
      $differences = $diff->getDiff($row->oldHtml, $row->newHtml); 
      if ($diff->isChanged($minLengthChange) || $force) { 
       $row->lastChanged = $row->lastUpdate; 
       $row->isChecked = false; 
       return ($differences); 
      } 
     } 
     return null; 
    } 

    function getConnection() { 
     try { 
      $pdoParams = array(
       PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true 
      ); 
      $db = new Zend_Db_Adapter_Pdo_Mysql(array(
         'host' => '127.0.0.1', 
         'username' => 'root', 
         'password' => 'administrator', 
         'dbname' => 'diff', 
         'driver_options' => $pdoParams 
        )); 
      return $db; 
     } catch (Exception $e) { 
      global $log; 
      $log->logError($e->getMessage()); 
     } 

    } 

回答

0

1)儘量使用提取方法,不使用fetchall:

foreach($sitesTable->fetch() as $row){ 
    //... 
} 

2)儘量沒有設置所有的變量,商店的HTML代碼(如果您將其保存在內存中),在最後一次迭代我假設變量$rowwithConnection裏面會有html代碼。

當我想要個人檔案php應用程序我使用xhprof它會爲您節省很多時間。祝你好運!