我得到:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 18635837 bytes) in /Users/[...]/cake/libs/cache/file.php on line 135
而且我不明白什么可以吃掉这么多内存。
我有很大的变量,其中包含大量的数组和数据。我的控制器以这样的方式结束:
// RENDER
$this->set(compact('var1', 'var2'));
debug(memory_get_usage()); // prints out: 33997240
33MB 远不及 134MB
如果我把debug(memory_get_usage());
它作为视图的第一行,我仍然会得到那个致命错误,这意味着问题不在视图的循环中。它似乎也不在控制器中,而是在控制器和视图之间。
如何调查问题所在并解决问题?
编辑<整个函数的代码:
function assignment_results($aid=null, $uid=null){
if($aid==null){
$this->Session->setFlash(__('Sorry but my butt got booted. 1907125790'));
$this->redirect($this->Misc->redirectHome());
}
$assignment = $this->EduAssignment->getById($aid);
// Get User IDS
if($uid==null){
$cus = $this->EduCourseUser->getStudentsForCourseId($assignment['EduAssignment']['edu_course_id']);
foreach ($cus as $cu){
$uids[]=$cu['EduCourseUser']['user_id'];
}
}else{
$uids[]=$uid;
}
// GET WORDING
$course = $this->EduCourse->getById($assignment['EduCourse']['id']);
$wt = $this->WritingTranslation->getById($assignment['EduAssignment']['writing_translation_id']);
$writing['Writing'] = $wt['Writing'];
if($writing['Writing']['type']== 'song' || $writing['Writing']['type']== 'video')
$this->paginate['limit'] = 2000;
$wording = $this->paginate('Word', array('Word.writing_translation_id'=>$wt['WritingTranslation']['id']));
$word_ids = array();
foreach($wording as $w){
$word_ids[]=$w['Word']['id'];
}
// CLICKS
$this->Click->unbindModel(array('belongsTo' => array('Word' )));
$clicks = $this->Click->getForAssignmentUserIds($assignment['EduAssignment']['id'], $uids);
// Assign clicks to words
foreach ($wording as &$wg){
$num = 0;
foreach ($clicks as $cl){
if($wg['Word']['id']==$cl['Click']['word_id']){
$num++;
}
}
$wg['Word']['click_number'] = $num;
}
// List of words by how many times clicked:
$wording_sorted = $wording;
// echo(memory_get_usage());
uasort($wording_sorted, array('TeachController', '_cmp'));
// debug(memory_get_usage());
// RENDER
$this->set(compact('writing', 'wording','wording_sorted', 'assignment', 'course'));
// debug(memory_get_usage());
}
function _cmp($a, $b){
return $a['Word']['click_number']<$b['Word']['click_number'];
}