PHP array consumes too much memory. In general, the memory usage of PHP arrays is only 110, that is, an array of MB of memory in C language requires 1 GB in PHP. Especially in the PHP backend server system, the memory usage of the PHP array is generally only 1/10, that is, an array of 100 MB memory in the C language requires 1 GB in PHP.
Especially in the system where PHP is used as the backend server, the memory consumption is too high.
Because this is a language problem, it is difficult to solve conventional solutions. The following is a solution using string.
Code
$ Total = 100000;
$ Double = "";
For ($ I = 0; $ I <$ total; $ I ++)
{
$ Double. = pack ("d", $ I + 0.1 );
}
For ($ I = 0; $ I <$ total; $ I ++)
{
Unpack ("@". ($ I * 8). "/d", $ double );
}
This example uses a string to save an array of double. And then unpack it during use.
Of course, this will affect performance. Depends on the specific requirements.
For example:
You have 10 arrays, each of which is 10 MB (about 1 million data records), so the ten arrays will spend MB of memory.
With the addition of 10 concurrent users, the memory will be severely insufficient.
Then, it is not used at the same time in 10 arrays. You can save them as strings.
Then, when used, unpack a string into an array.
Listen 1/10, that is to say, an array of 100 MB memory in C language requires 1 GB in PHP. Especially in PHP as the background server system...