Solution to the problem of PHP array memory consumption

  • 2020-03-31 20:32:37
  • OfStack

Because this is a language problem, the conventional solution is more difficult to solve. The following is a string solution.
 
$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 hold an array of doubles. Then unpack it out while using it.
Of course, this can affect performance. It depends on the specific needs.

For example:
If you have 10 arrays, each of which is 10 megabytes (about 1 million bits of data), then ten will cost 100 megabytes of memory.
Add 10 concurrent people and you run out of memory.
And then, in 10 arrays, not all at once. You can save them as strings
Then, when used, one of the unpack strings becomes an array.

Related articles: