php数组循环占用内存,php – 每次循环后内存使用量增加

我有一个foreach循环,读取一个包含大约200000行的文件,这是我的

PHP:

foreach ($this->file as $row)

{

if ($this->file->valid())

{

//init client array

$this->initInvoicesArray($row);

$this->prepareInvoice();

$row = null;

$this->key = $this->file->key();

//add msisdn to msisdn array, and client to clients array

self::$MsisdnArray[] = $this->msisdn;

self::$InvoicesArray[$this->msisdn] = $this->client;

if ($i % 3000 == 0)

{

//get valid users from table

$this->prepareAndSaveValidClients();

}

$i++;

}

}

这是prepareAndSaveValidClients()函数:

public function prepareAndSaveValidClients(){

$query = Doctrine_Query::create()

->select('p.gender, p.email2, u.username, u.first_name, u.last_name, u.email_address, u.is_active, p.msisdn, p.user_id, p.city_id, p.street, p.zipcode, p.msisdn_status')

->from('sfGuardUser u')

->innerJoin('u.Profile as p ON u.id = p.user_id')

->whereIn('p.msisdn', self::$MsisdnArray)

->whereIn('p.status', self::$AllowedStatus);

$results = $query->fetchArray();

//instanciat an object collection for payment_notifications

$collection = new Doctrine_Collection("payment_notifications");

if (!empty($results))

{

foreach ($results as $key => $client)

{

$invoice = self::$InvoicesArray[$client['Profile']['msisdn']];

$this->initInvoicesArray($invoice);

$this->prepareInvoice();

$this->prepareUserProfile($client);

$this->prepareClient();

$paymentNotifications = new paymentNotifications();

$paymentNotifications->fromArray($this->client);

$collection->add($paymentNotifications);

$tel = $client['Profile']['msisdn'];

$client = null;

}

$collection->save();

//clear memory

$results = null;

$collection = null;

self::$MsisdnArray = null;

self::$InvoicesArray = null;

$this->logSection('tel num', 'added :' . $tel . ' Memory usage : ' . memory_get_usage());

$duration = microtime(true) - $this->startTime;

$this->logSection('payment : ', sprintf('added in %s', $duration));

}

}

至于功能:

$this->initInvoicesArray($invoice);

$this->prepareInvoice();

$this->prepareUserProfile($client);

$this->prepareClient();

它们只是用于准备$this->客户端

这是内存使用情况,因为它显示在每个循环中:

>> tel num added :0699946185 Memory usage : 89287596

>> payment : added in 8.6373870372772

>> tel num added :0699983919 Memory usage : 165854544

>> payment : added in 18.373502969742

>> tel num added :0699949623 Memory usage : 241338788

>> payment : added in 29.336947917938

>> tel num added :0699854750 Memory usage : 319173092

>> payment : added in 40.880628824234

如您所见,我试图释放这些变量的记忆:

$results = null;

$collection = null;

self::$MsisdnArray = null;

self::$InvoicesArray = null;

但是徒劳无功,每次循环后内存使用量都会不断增加,从而导致“允许的内存大小”出现致命错误.我该如何优化它?

谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值