PHP array memory consumption too much problem solution
Source: Internet
Author: User
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 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.
The code is as follows:
$ 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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service