PHP sudden increase of memory usage during array operations

kijin

I'm writing a maintenance script in PHP. The script keeps about 100,000 key-value pairs in an associative array and compare a bunch of other data with that array.

The keys are 12- or 16-byte hexademical strings.

The values are arrays containing 1-10 strings. Each string is around 50 bytes.

I'm populating my array by reading a text file line-by-line with fgets() in a loop.

All is fine until I hit about 44,000 keys, but after that the memory usage suddenly skyrockets.

No matter how much I increase the memory limit (and I'm relunctant to give it any more than 256MB at the moment), the memory usage increases exponentially until it hits the new limit. This is weird!

The following is a table with the number of keys on the left and the memory usage on the right.

10000     6668460
20000    12697828
30000    18917768
40000    25045068
41000    25658148
42000    26760304
43000    27350368
44000    27920400
45000    33438520
46000    77800344
47000   114203960
48000   161989660
49000   168419992
50000   206265572
Fatal error: Allowed memory size of 268435456 bytes exhausted

As you can see, the memory usage is consistent at 620-660 bytes per key until I reach 44,000 keys. After that, memory usage suddenly begins to increase, until it reaches over 4KB per key at 50,000 keys. This is very strange because the size of my keys and values are always similar.

It seems like I'm hitting some sort of internal limit on the number of keys I can have in an array, beyond which it all becomes very inefficient.

If I can maintain a memory usage of 620-600 bytes per key (which sounds reasonable given the usual overhead of using an array), my entire dataset should fit in approx. 64MB of memory and therefore easily accessible when I need to reference it later in the same script. This was the assumption when I first started writing the script. It's a maintenance script run from the CLI, so it's OK to use 64MB of memory from time to time.

But if the memory usage keeps increasing like the above, I'll have no choice but to offload the key-value dataset to an external daemon like Memcached, Redis, or an SQL database, and the network overhead will greatly slow down the maintenance script.

What I tried so far:

  • I tried flattening the two-dimensional array into multiple one-dimensional array. No luck.
  • I tried splitting the large array into multiple smaller arrays. No luck.
  • I tried not using arrays at all and turning every key into a separate variable. No luck.
  • I can't use SplFixedArray because my keys are not numeric (and can't be converted to numbers within the integer range) and the array needs to be mutable.
  • I would rather not use Quickhash, Judy, or any of the other alternative array implementations written as a C extension.
  • Sorry, this script needs to be in PHP. Don't ask me why...

The test server is a virtual machine running Ubuntu 12.04 LTS, 32-bit, with PHP 5.3.10-1ubuntu3.9.

Any ideas?

  • Is this something that is fixed in a more recent version of PHP?
  • Should I just give the dataset to an external daemon like Memcached?

Thanks!

dkellner

It's the garbage collection I think. At some point you're using operations that allocate temporary space which then cannot be freed during the hard work so php will eat up your memory for no real purpose.

When I faced this problem I finally got to the conclusion that garbage is being thrown away at specific events only, like exiting a function. So what you should try is, make the job in several smaller steps and let your variables "relax" between them - make a function that only does a thousand elements at a time, then call it again to continue where it left off.

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP Array Memory Usage

From Dev

mysqli_result::free increase php memory usage

From Dev

mysqli_result::free increase php memory usage

From Java

Strange PHP memory usage in array of strings

From Dev

Array / memory operations in Swift

From Java

Memory Usage Increase Approximately Double for Assigning 1

From Dev

Android memory usage increase when a button is pressed

From Dev

Android memory usage increase when a button is pressed

From Dev

How does PHP’s array memory usage management work?

From Java

Need an array-like structure in PHP with minimal memory usage

From Dev

Benchmark memory usage in PHP

From Dev

Memory usage of php process

From Dev

Benchmark memory usage in PHP

From Dev

PHP unexpected memory usage

From Dev

PHP CPU and Memory Usage

From Dev

Unable to increase PHP Memory Limit

From Dev

Memory leak: steady increase in memory usage with simple device motion logging

From Dev

Memory leak: steady increase in memory usage with simple device motion logging

From Dev

Decreasing memory usage for array and SplFixedArray

From Dev

Memory usage in R array assignment

From Dev

Memory usage of byte array in Java

From Dev

How to avoid fast memory increase during scavenge gc?

From Dev

Assigning names to large objects appears to increase memory usage considerably

From Dev

Why Google Chrome increase the memory usage on each page reload?

From Dev

Increase in Memory usage when dispatch is called in a setTimeout loop

From Dev

Why Google Chrome increase the memory usage on each page reload?

From Dev

Windows Task Manager: Python app memory usage increase

From Dev

Does casting of a primitive type increase/decrease memory usage?

From Dev

Why not memory address of an array always increase?

Related Related

  1. 1

    PHP Array Memory Usage

  2. 2

    mysqli_result::free increase php memory usage

  3. 3

    mysqli_result::free increase php memory usage

  4. 4

    Strange PHP memory usage in array of strings

  5. 5

    Array / memory operations in Swift

  6. 6

    Memory Usage Increase Approximately Double for Assigning 1

  7. 7

    Android memory usage increase when a button is pressed

  8. 8

    Android memory usage increase when a button is pressed

  9. 9

    How does PHP’s array memory usage management work?

  10. 10

    Need an array-like structure in PHP with minimal memory usage

  11. 11

    Benchmark memory usage in PHP

  12. 12

    Memory usage of php process

  13. 13

    Benchmark memory usage in PHP

  14. 14

    PHP unexpected memory usage

  15. 15

    PHP CPU and Memory Usage

  16. 16

    Unable to increase PHP Memory Limit

  17. 17

    Memory leak: steady increase in memory usage with simple device motion logging

  18. 18

    Memory leak: steady increase in memory usage with simple device motion logging

  19. 19

    Decreasing memory usage for array and SplFixedArray

  20. 20

    Memory usage in R array assignment

  21. 21

    Memory usage of byte array in Java

  22. 22

    How to avoid fast memory increase during scavenge gc?

  23. 23

    Assigning names to large objects appears to increase memory usage considerably

  24. 24

    Why Google Chrome increase the memory usage on each page reload?

  25. 25

    Increase in Memory usage when dispatch is called in a setTimeout loop

  26. 26

    Why Google Chrome increase the memory usage on each page reload?

  27. 27

    Windows Task Manager: Python app memory usage increase

  28. 28

    Does casting of a primitive type increase/decrease memory usage?

  29. 29

    Why not memory address of an array always increase?

HotTag

Archive