Strategies for handling memory consumption in PHP5?

We have a large management software that produces large reports of all kinds, based on numerous cycles, with database extraction, object creation (many), etc.

On PHP4, it can work successfully with a memory capacity of 64 MB - now we have moved it to a new server and with the same database - the same code, the same reports will not appear without limiting the memory size ...

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

The question at the end is what strategies do you use when you need to have your own diet scenarios?

+6
php memory
source share
5 answers

The big problem we are facing is circular references between objects, which prevent them from freeing up memory when they become inaccessible.

Depending on your architecture, you can use __destruct () and manually disable any links. For our problem, I ended up restructuring classes and removing circular references.

+6
source share

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

In addition, I recommend you the following articles:

+6
source share

With the transition to the new server, have you confirmed that your MySQL and PHP system variables are identical to the ones they were on your old server?

PHP5 introduced many new features, but because of its backward compatibility mantra, I don’t think that the differences between PHP5 and PHP4 should lead to a big impact on the performance of an application that has not changed its code and database.

Are you also running the same version of Apache or IIS?

This seems like a problem that is more likely related to your new system environment than upgrading from PHP4 to 5.

0
source share

Bertrand

If you are interested in refactoring existing code, I would recommend that you first control the use of your CPU and memory while reporting. Will you block your SQL server or block Apache (what happens if PHP code is inserted into the system)?

I was working on a project that initially strangled MySQL so much that we had to reorganize the entire process of creating the report. However, when we were done, the download was just ported to Apache (via more complex PHP code). Our final decision was to reorganize the database design to provide better performance for reporting functions and use PHP to take up the slack in what we couldn't do initially in MySQL.

Depending on the nature of the reports, you might consider denormalizing the data that is used for the reports. You might even consider creating a second database, which serves as a data warehouse, and is designed for OLAP principles, not OLTP principles. You can start on Wikipedia for a general explanation of OLAP and data warehousing.

However, before you start looking at serious refactoring, you have confirmed that your environments are quite similar by looking at phpinfo (); for PHP and SHOW VARIABLES; in MySQL?

0
source share

Giant!?!

even 64 MB is big.

Ignoring the mismatch between the environments (which sounds very strange), it seems that the code may require some re-factoring.

any chance you can modify your code so that the result sets from database queries are not dumped into arrays. I would recommend you create an iterator for your result sets. (from there you can consider them as an array for most purposes). There is a big difference between processing one record at a time and processing 10,000 records at a time.

secondly, take a look at the weather when your code creates multiple instances of data. Can you pass the objects by reference. (use '&'). We should have done a similar thing using an early version of the horde frame. a 1 MB application would blow up to 50 MB from multiple calls that transmitted the entire data set as a copy, rather than as a link.

0
source share

All Articles