Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategies for handling memory consumption in PHP5?

Tags:

php

memory

We have a large management software that is producing big reports of all kinds, based on numerous loops, with database retrievals, objects creations (many), and so on.

On PHP4 it could run happily with a memory limit of 64 MB - now we have moved it on a new server and with the same database - same code, the same reports won't come up without a gig of memory limit...

I know that PHP5 has changed under the hood quite a lot of things, but is there a way to make it behave ?

The question at the end is, what strategies do you apply when you need to have your scripts on a diet ?

like image 535
Bertrand Gorge Avatar asked Oct 23 '08 21:10

Bertrand Gorge


2 Answers

A big problem we have run into was circular references between objects stopping them from freeing memory when they become out of scope.

Depending on your architecture you may be able to use __destruct() and manually unset any references. For our problem i ended up restructuring the classes and removing the circular references.

like image 140
MOdMac Avatar answered Nov 19 '22 16:11

MOdMac


When I need to optimize resources on any script, I try always to analyze, profile and debug my code, I use xDebug, and the xDebug Profiler, there are other options like APD, and Benchmark Profiler.

Additionally I recommend you this articles:

  • Make PHP apps fast, faster, fastest..
  • Profiling PHP Applications (PDF)
  • PHP & Performance (PDF)
like image 6
Christian C. Salvadó Avatar answered Nov 19 '22 16:11

Christian C. Salvadó