PHP array memory consumption too much problem solution _ PHP Tutorial

Source: Internet
Author: User
PHP array memory consumption too much problem solution. Because this is a language problem, it is difficult to solve conventional solutions. The following is a solution using string. Copy the code as follows: $ total100000; $ double; for ($ I is difficult to solve because it is a language problem. 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.

Bytes. The following is a solution using string. The code is as follows: $ total = 100000; $ double = ""; for ($ I =...

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.