PHP arrays consume too much memory to solve the problem

Source: Internet
Author: User
Tags arrays unpack

In general, the PHP array memory utilization is only 1/10, that is, a C language inside the 100M memory array, in PHP will be 1G.

Especially in the system of PHP as a background server, often the problem of memory consumption is too big.

Because this is a language problem, the general scheme is difficult to solve. The following is a scenario that is resolved by 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 holds an array of double in a string. Then unpack it out at the time of use.

Of course, this can affect performance. Depends on the specific needs.

For example, in this case:

You have 10 arrays, 10M per array (around 1 million data), then 10 will cost 100M of memory.

Plus 10 people concurrency, memory will be severely inadequate.

Then, in 10 arrays, not all of them are used at the same time. You can save them as a string.

Then, when used, unpack a string into an array.

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.