Home >Backend Development >PHP Problem >Don't php arrays pass through memory?

Don't php arrays pass through memory?

(*-*)浩
(*-*)浩Original
2019-09-28 16:23:221944browse

Generally speaking, the memory utilization of PHP arrays is only 1/10. That is to say, an array with 100M of memory in C language requires 1G in PHP.

Don't php arrays pass through memory?

Especially in systems where PHP is used as a backend server, the problem of excessive memory consumption often occurs.

Because this is a language problem, conventional solutions are more difficult to solve. The following is a solution through string. (Recommended learning: PHP video tutorial)

$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 doubles. Then unpack it when using it.

Of course, this will affect performance. It depends on the specific needs.

For example, in this case:

You have 10 arrays, each array is 10M (about 1 million data), then ten will cost 100M of memory.

If you add 10 people concurrently, the memory will be seriously insufficient.

Then, among the 10 arrays, they are not used at the same time every time. You can save them as string

and then, when using it, unpack a certain string into an array.

The above is the detailed content of Don't php arrays pass through memory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn