问题描述
我正在尝试使用Google API在地图上显示用户.现在,当用户数增加到12000时,我遇到了内存异常错误.我暂时将内存从128增加到256.但是我可以肯定的是,当它的25000个用户再次出现时,也会出现相同的错误.
I am trying to display users on a map using google API. Now that when users count increase to 12000 I got an memory exception error. For the time being I increased the memory to 256 from 128. But I am sure when its 25000 users again the same error will come.
错误是,/var/www/html/digitalebox_v2_20150904_100/protected/extensions/bootstrap/widgets/TbBaseMenu.php on
行193 中的
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 30720 bytes)
致命错误:类声明可能不嵌套in
/var/www/html /yii-1.1.14.f0fee9/framework/collections/CListIterator.php on
第20行`
The error is,Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 30720 bytes)
in /var/www/html/digitalebox_v2_20150904_100/protected/extensions/bootstrap/widgets/TbBaseMenu.phpon
line 193Fatal error: Class declarations may not be nested
in
/var/www/html/yii-1.1.14.f0fee9/framework/collections/CListIterator.phpon
line 20`
我的代码,
$ criteria =新的CDbCriteria();$ criteria-> condition ='t.date BETWEEN:from_date AND:to_date';
$ modelUsers = User :: model()-> findAll($ criteria); foreach($ modelUsers as $ modelUser){ $ fullAddress = $ modelUser-> address1. ','. $ modelUser-> zip. ''. $ modelUser-> city. ','.国家:: model()-> findByPk($ modelUser-> countryCode)-> countryName; }
$criteria = new CDbCriteria();$criteria->condition = 't.date BETWEEN :from_date AND :to_date';
$modelUsers = User::model()->findAll($criteria); foreach ($modelUsers as $modelUser) { $fullAddress = $modelUser->address1 . ', ' . $modelUser->zip . ' ' . $modelUser->city . ', ' . Country::model()->findByPk($modelUser->countryCode)->countryName; }
当此$modelUsers
具有12000条记录时,此内存问题是由于其12000个用户对象引起的.
when this $modelUsers
has 12000 records this memory problem comes as its 12000 user objects.
我应该怎么做才能防止此类问题的发生? php应用程序运行所需的最小内存量是多少?
what should i do to prevent this kind of issues in future ? what is the minimum required memory size for a php application to run ?
推荐答案
调用findAll会及时加载所有记录,因此会出现最终内存错误.对于这种情况,Yii特别具有 CDataProviderIterator .它允许对大型数据集进行迭代,而无需将整个数据集保存在内存中.
When you call findAll it loads all records in time, so you get end memory error. Speacialy for that situations Yii has CDataProviderIterator. It allows iteration over large data sets without holding the entire set in memory.
$dataProvider = new CActiveDataProvider("User");
$iterator = new CDataProviderIterator($dataProvider);
foreach($iterator as $user) {
$fullAddress = $modelUser->address1 . ', ' . $modelUser->zip . ' ' . $modelUser->city . ', ' . Country::model()->findByPk($modelUser->countryCode)->countryName;
}
这篇关于PHP允许的内存大小内存大小用尽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!